|
export default function useLocalStorageState(key, initial) { |
|
if (typeof window !== "undefined") { |
|
// Try and get some values from localStorage: |
|
// Set up defaults if there's nothing in localStorage (i.e. the values are null): |
|
const [value, setValue] = useState(() => { |
|
return JSON.parse(window.localStorage.getItem(key)) || initial; |
|
}); |
|
|
|
// localStorage is a side-effect: keep track of these |
|
useEffect(() => { |
|
window.localStorage.setItem(key, JSON.stringify(value)); |
|
}, [value]); |
|
|
|
return [value, setValue]; |
|
} |
|
} |
Nice to see that you're using a custom hook to access the local storage. Such a good idea! 馃摝 馃ぉ
alis-learning/components/Hooks/useLocalStorageState.js
Lines 3 to 18 in 47899f1
Nice to see that you're using a custom hook to access the local storage. Such a good idea! 馃摝 馃ぉ