Read the task guideline before you start.
❗️❗️❗️ BEFORE OPENING A PULL REQUEST, PROOF‑READ YOUR CODE WITH THE CHECKLIST. ❗️❗️❗️
s Custom‑hook challenge, but this time the focus is persistence.
Create and export a useLocalStorageState React hook that turns any piece of component state into durable state backed by localStorage.
const [state, setState, reset] = useLocalStorageState(
key, // string – localStorage key
defaultValue, // any – fallback when key missing or invalid
options // optional { serialize, deserialize }
);serialize(value)– converts any value to a string (defaults toJSON.stringify).deserialize(raw)– converts the stored string back to a value (defaults toJSON.parse).reset()– clears the key and putsstateback todefaultValue.
Simplified brief: No cross‑tab synchronisation, no expiry logic, no extras. Just persist and reset.
-
Initial value On the first render, check
window.localStorage:- If the key exists, read and deserialize it.
- If not, fall back to
defaultValue.
-
Persistence Whenever
statechanges, serialize it and write tolocalStorageunder the same key. -
Reset helper The
resetfunction should:- Remove the key from
localStorage. - Set React state back to
defaultValue.
- Remove the key from