Redux ~ Create Store - rohit120582sharma/Documentation GitHub Wiki

A store holds the whole state tree of your application. The only way to change the state inside it is to dispatch an action on it.

State (also called the state tree) is a broad term, but in the Redux API it usually refers to the single state value that is managed by the store and returned by getState(). It represents the entire state of a Redux application, which is often a deeply nested object.

A store is not a class. It's just an object with a few methods on it. To create it, pass your root reducing function to createStore.

If you're coming from Flux, there is a single important difference you need to understand. Redux doesn't have a Dispatcher or support many stores. Instead, there is just a single store with a single root reducing function.


Arguments

  • reducer (Function): A reducing function that returns the next state tree, given the current state tree and an action 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.
  • enhancer (Function): The store enhancer. You may optionally specify it to enhance the store with third-party capabilities such as middleware, time travel, persistence, etc. The only store enhancer that ships with Redux is applyMiddleware().

Returns

It returns an object (Store) that holds the complete state of your app. The only way to change its state is by dispatching actions. You may also subscribe to the changes to its state to update the UI.


Tips

  • Don't create more than one store in an application! Instead, use combineReducers to create a single root reducer out of many.
  • It is up to you to choose the state format. You can use plain objects or something like Immutable.
  • When a store is created, Redux dispatches a dummy action to your reducer to populate the store with the initial state. You are not meant to handle the dummy action directly. Just remember that your reducer should return some kind of initial state if the state given to it as the first argument is undefined, and you're all set.
  • To apply multiple store enhancers, you may use compose().


⚠️ **GitHub.com Fallback** ⚠️