createStore(reducer, [preloadedState], [enhancer])
Arguments
(Function): A that returns the next state tree, given the current state tree and an to handle.
[
preloadedState
] (any): The initial state. You may optionally specify it to hydrate the state from the server in universal apps, or to restore a previously serialized user session. If you producedreducer
withcombineReducers
, this must be a plain object with the same shape as the keys passed to it. Otherwise, you are free to pass anything that yourreducer
can understand.
Returns
(Store
): An object that holds the complete state of your app. The only way to change its state is by . You may also subscribe to the changes to its state to update the UI.
Example
Tips
If your state is a plain object, make sure you never mutate it! For example, instead of returning something like
Object.assign(state, newData)
from your reducers, return . This way you don't override the previousstate
. You can also writereturn { …state, …newData }
if you enable the .For universal apps that run on the server, create a store instance with every request so that they are isolated. Dispatch a few data fetching actions to a store instance and wait for them to complete before rendering the app on the server.
To apply multiple store enhancers, you may use
compose()
.