I made a hook for making this sort of thing easier.
You would just need to do something like.
const [state, setState] = useState(); useEffect(() => { const setHandler = (key) => { if(key === "state") setState(storage.get("state")); } storage.on('set', setHandler); return () => { storage.off('set', setHandler); } })
Which will mean, provided you use the storage context, the state will always be in sync with local/session storage.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I made a hook for making this sort of thing easier.
React: Custom hook for accessing storage
Andrew Bone ・ Apr 21 ・ 8 min read
You would just need to do something like.
Which will mean, provided you use the storage context, the state will always be in sync with local/session storage.