Implement a custom hook to get and set values from and to Local Storage. A custom hook is a way (or rather a mechanism) to reuse stateful logic. State and effects inside a custom hook are completely isolated.
useLocalStorage
that a getter value and a setter method.const [storedValue, setStoredValue] = useLocalStorage('name', 'initialValue');
useLocalStorage
hook should be the key name
that should be set and retrieved from the local storage.useLocalStorage
hook should be the initial value
that the local storage key accepts.select
tag dropdown with two values, light
and dark
(these are the options inside of the select)stay the same
because we are essentially storing the value in local storage.Click to reveal
A custom hook has its own useEffect
and useState
logic.
Click to reveal
To set a value in localStorage, use setItem()
method on window.localStorage
object. Do not forget to JSON.stringify()
your values.
Click to reveal
To get a value from localStorage, use getItem()
method on window.localStorage
object. Do not forget to JSON.parse()
your values.