Thinking in Redux
Core conception
Describe state with normal object. Dispatch an action to update the data of state.
Use some functions to connect action with state, that is reducer.
Add another reducer to call the reduces above, to manage the whole state of application:
Principles
- One way data flow
- Appliction’s state is maintained in a object tree, which exists unique in stores。
- State is only readable
- The only way to change state is dispatch an action,which is an object to descrbie what happened.
- Action just describe the fact that something happened, do not describe how to update the state.
Action is an object, and can be logged、serialized、stored.
- use pure function to modify
Action
Action is the only datasource of store. Use store.dispatch()
to deliver action into store.
Use action generate function in Redux, to return an action:
Reducer
Reducers specify how changes to the application state are sent to the store in response to actions.
Reducer is pure function,with prev state and prev action as params, to return new state:
Split reducers
equals to :
Store
Store’ works:
- maintain states of application;
- provide
getState()
to get state; - provide
dispatch(action)
to update state; - use
subscribe(listener)
to add listener;
Data Flow
- call
store.dispatch(action)
- Redux store call the incoming reducer function
- root reducer combine chidren reducers to a single state tree。
- Redux store mantain the root reducer to return the whole state。