DEV Community

Discussion on: Using useContext and useState hooks as a store

Collapse
 
pazsea profile image
Patrick Sjöberg • Edited

I was looking for this. How to share multiple states in context using useContext.
I thought about passing value as
{[firstState, setFirstState, secondState, setSecondState]}

This works but it was very ugly when I deconstructed it inside a child component. Your way is way more elegant, thank you!

Collapse
 
ascotto profile image
Andrea Scotto Di Minico • Edited

@nazmifeeroz nice approach on how to decouple multiple stores with useState, I had a similar approach but instead used reducers in a similar way as Redux.

As you metioned @pazsea , with useState get complicated quickly.

As I was solving similar issues in the past, recently I wrote a tutorial on how to use Context and that code is decoupled with similar structure as Redux you can have a look here: frontendbyte.com/how-to-use-react-...

Of course I would be happy to know if it was helpful too.

Cheers,
Andrea