3 ms·
I don't think key should be in the last part of here, because key should never change inside the closure: React.useEffect(() => { window.localStorage
by benatkin 7y ago
I don't think key should be in the last part of here, because key should never change inside the closure:
React.useEffect(() => {
window.localStorage.setItem(key, JSON.stringify(value));
}, [key, value]);
It's pretty neat because it has a simple and familiar API. I think it's more like sticky props than sticky state, though...
Edit: here's another hook with a more accurate, less catchy name, plus error handling: https://usehooks.com/useLocalStorage/ https://usehooks.com/useLocalStorage/
- ricardobeat 7y agoThe ESLint rules for hooks enforce adding any values used inside the function to the dependency array.