useLocalStorage Hook — Persist State to Local Storage

Custom Hooks — Intermediate React challenge.

Build a generic useLocalStorage hook from scratch, then use it to power a persisted counter with Increment, Set-to-100, and Reset — no libraries, no pre-built markup.

Requirements

Signature

function useLocalStorage(key: string, initialValue: any): [any, (value: any) => void, () => void]

Example

const [count, setCount, resetCount] = useLocalStorage("reactgrind-counter-demo", 0);
// setCount(prev => prev + 1) -> increments and persists
// setCount(100) -> sets directly and persists
// resetCount() -> clears storage, count back to 0

Constraints

🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code
Loading workspace…