#
Redux FAQ: Store Setup#
Table of Contents- Redux FAQ: Store Setup
- Table of Contents
- Store Setup
- Can or should I create multiple stores? Can I import my store directly, and use it in components myself?
- Is it OK to have more than one middleware chain in my store enhancer? What is the difference between
next
anddispatch
in a middleware function? - How do I subscribe to only a portion of the state? Can I get the dispatched action as part of the subscription?
#
Store Setup#
Can or should I create multiple stores? Can I import my store directly, and use it in components myself?The original Flux pattern describes having multiple “stores” in an app, each one holding a different area of domain data. This can introduce issues such as needing to have one store “waitFor
” another store to update. This is not necessary in Redux because the separation between data domains is already achieved by splitting a single reducer into smaller reducers.
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 React Redux, 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 <Provider store={store}>
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 informationDocumentation
Discussions
- #1346: Is it bad practice to just have a 'stores' directory?
- Stack Overflow: Redux multiple stores, why not?
- Stack Overflow: Accessing Redux state in an action creator
- Gist: Breaking out of Redux paradigm to isolate apps
next
and dispatch
in a middleware function?#
Is it OK to have more than one middleware chain in my store enhancer? What is the difference between 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 applyMiddleware
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 informationDocumentation
Discussions
- #1051: Shortcomings of the current applyMiddleware and composing createStore
- Understanding Redux Middleware
- Exploring Redux Middleware
#
How do I subscribe to only a portion of the state? Can I get the dispatched action as part of the subscription?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 getState()
to get the current state value.
This API is intended as a low-level primitive with no dependencies or complications, and can be used to build higher-level subscription logic. UI bindings such as React Redux can create a subscription for each connected component. It is also possible to write functions that can intelligently compare the old state vs the new state, and execute additional logic if certain pieces have changed. Examples include redux-watch, redux-subscribe and redux-subscriber which offer different approaches to specifying subscriptions and handling changes.
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 informationDocumentation
Discussions
- #303: subscribe API with state as an argument
- #580: Is it possible to get action and state in store.subscribe?
- #922: Proposal: add subscribe to middleware API
- #1057: subscribe listener can get action param?
- #1300: Redux is great but major feature is missing
Libraries