Intermediate
useLocalStorage: remember the name
Write useLocalStorage(key, initialValue) that works like useState but reads the saved value (stored as JSON) on the first render and saves the value as JSON whenever it changes.
The starter saves "Guest" under the key username before the app starts, as if the user came before. Use the hook with that key: the input (id="name") must start with Guest, and the greeting id="greeting" says Hello, <name>!. Typing a new name must save it to localStorage as JSON ("Asha" with quotes).
What Check tests
- The saved name
Guestis loaded on the first render - Typing updates the greeting
- The new name is saved as JSON under
username - The code defines
useLocalStorageand uses it forusername
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
Runs in your browser. Your code is saved here.
Your page
Press Run to see the page.
Console
Nothing logged yet.