今天学习了阮一峰大大的react-redux的store API ,有一些理解不知道对不对,记一下笔记
- store.getState() 拿到当前时刻的state
- store.dispatch() 可以设置,修改state的值
- store.subscribe() 设置监听函数,一旦 State 发生变化,就自动执行这个函数。
工作流程
1.首先,用户发出 Action。
store.dispatch(action);
2.然后,Store 自动调用 Reducer,并且传入两个参数:当前 State 和收到的 Action。 Reducer 会返回新的 State 。
let nextState = todoApp(previousState, action);
State 一旦有变化,Store 就会调用监听函数。
// 设置监听函数
store.subscribe(listener);
3.listener可以通过store.getState()得到当前状态。如果使用的是 React,这时可以触发重新渲染 View。
function listerner() {
let newState = store.getState();
component.setState(newState);
}