Redux FAQ: Store Setup
Store Setup
As with several other questions, it is possible to create multiple distinct Redux stores in a page, but the intended pattern is to have only a single store. Having a single store enables using the Redux DevTools, makes persisting and rehydrating data simpler, and simplifies the subscription logic.
Some valid reasons for using multiple stores in Redux might include:
- Solving a performance issue caused by too frequent updates of some part of the state, when confirmed by profiling the app.
- Isolating a Redux app as a component in a bigger application, in which case you might want to create a store per root component instance.
However, creating new stores shouldn't be your first instinct, especially if you come from a Flux background. Try reducer composition first, and only use multiple stores if it doesn't solve your problem.
Similarly, while you can reference your store instance by importing it directly, this is not a recommended pattern in Redux. If you create a store instance and export it from a module, it will become a singleton. This means it will be harder to isolate a Redux app as a component of a larger app, if this is ever necessary, or to enable server rendering, because on the server you want to create separate store instances for every request.
With , the wrapper classes generated by the connect()
function do actually look for props.store
if it exists, but it's best if you wrap your root component in and let React Redux worry about passing the store down. This way components don't need to worry about importing a store module, and isolating a Redux app or enabling server rendering is much easier to do later.
Further information
Documentation
Discussions- Stack Overflow: Redux multiple stores, why not?
- Gist: Breaking out of Redux paradigm to isolate apps
Redux middleware act like a linked list. Each middleware function can either call next(action)
to pass an action along to the next middleware in line, call dispatch(action)
to restart the processing at the beginning of the list, or do nothing at all to stop the action from being processed further.
This chain of middleware is defined by the arguments passed to the function used when creating a store. Defining multiple chains will not work correctly, as they would have distinctly different dispatch
references and the different chains would effectively be disconnected.
Further information
Documentation
Redux provides a single store.subscribe
method for notifying listeners that the store has updated. Listener callbacks do not receive the current state as an argument—it is simply an indication that something has changed. The subscriber logic can then call to get the current state value.
The new state is not passed to the listeners in order to simplify implementing store enhancers such as the Redux DevTools. In addition, subscribers are intended to react to the state value itself, not the action. Middleware can be used if the action is important and needs to be handled specifically.
Further information
Documentation