DEV Community

Discussion on: Build a Redux-like Global Store Using React Hooks

Collapse
 
visarts profile image
Brendan B

Hey I have a question -- I really like this, and implemented something similar in my own app, but I've noticed that because I use the 'useStore' call in a bunch of different components, it's getting called dozens of times on load. Do you think this will be a significant issue at scale or is there a way to implement it so doesn't need to get called over and over to fetch values?

Collapse
 
ramsay profile image
Ramsay

Hi Brendan! In production at work, we have actually moved dispatch into its own context and have a useDispatch and useStore hook. Usually, components needed to dispatch actions don't generally need to use the state itself. This will probably cut down on a lot of the re-renders you're seeing.

Another option for possibly reducing the amount of calls would be to create a "hydrate store" action that does all the initial loading of state you need in one action. I'm not sure how feasible this is in your project, but its something we're doing as well.

Collapse
 
visarts profile image
Brendan B

Thanks for the reply! This definitely gives me something to chew on

Collapse
 
juwdohr profile image
Juwdohr

Ramsay, loved your Article. I actually was able to follow it and implement it on something that I have been working on. Wondering you how you would have created the useDispatch and the "hydrate store" action?