A store holds the whole state tree of your application.
What is State?
State represents the entire state of a Redux application, which is often a deeply nested object.
The only way to change the state is to dispatch an action. Its an object with few methods on it.
Store Methods:
1. getState() :
This method returns current state tree of application. reducers returns the state since it is pure function. So current state is nothing but the last state return but the reducer function.
Return type of getState() is any.
2. dispatch(action)
This Method dispatches an action. This is the only way to change the state.
The parameter is action. action is nothing but an object which contains property like action type and payload.
3.subscribe(listener)
This method use to subscribe the store. the parameter for this method is callback function.if there is any change happen to the state by dispatching any action then listener callback function will get call.This method return a function which unsubscribe the change listener.
Only way to change the state in store is dispatch an action. all dispatched actions are handled in the reducer. According to action type reducer construct the new state and return it to store.When state get change all subscriber who subscribe to the store call the listener.
Store is single source of truth so its very easy to debug an application.
This is all about state. In short State is simple tree like object .It is like in-memory database of an application. there are different methods to update the state and listen to state change.
In upcoming post we will see Reducers & Effects. Stay tuned. Happy Coding 😊 !!!
Comments
Post a Comment