DEV Community

Discussion on: Writing a custom react hook that persists to local storage

Collapse
 
patroza profile image
Patrick Roza • Edited

Interesting to use the useEffect hook aswell, though you can also create a function that combines calling the store.set and state set. Not sure which yet which i’d prefer.

What’s your thoughts on using context, instead of useState? useState and store provide a similar function but with context it becomes something that can be shared, which would address the case of having multiple consumers of the state, each updating accordingly when the storage is updated.
Perhaps a refactoring for when the need arises.

Collapse
 
mcrowder65 profile image
Matt Crowder

Yeah! Before I wrote this article, I actually wasn't using useEffect at all! And I was just invoking whenever the setter was invoked: github.com/mcrowder65/mooks/commit...

Context is great, but underneath, it would still need to do useState. I would be happy to provide an example on how to do useContext.