数据流:
用户发出action,出发reducer
reducer接收state和action,来根据actino去修改state,然后触发订阅队列。
通过getState() 来获取 当前的state。
redux知识点:
reducer:
1.用来接收state和action,根据action来修改state,返回新的state。
2.提供state初始值。
store:
createrStore 接收:reducer、state、enhancer, 返回dispatch getstate subscribe
stroe.js:dispatch({ actionTypes : init}) 用来初始化state
dispatch: 接收一个action对象,并且把createrStore 接收到的state,传递给了reducer并且执行,并且把reducer返回的state赋值给currentState,最后执行订阅队列中的方法。这里的currentState就是getState获取到的对象。
redux 和 react 关联:
上级组件宣称自己支持context,并提供一个函数返回包含store的context对象。
子组件宣称自己需要context,就可以通过this.context 访问共同的对象。
在react-redux中,Provider:提供包含store的context,connect:把state转换为内层组建的props,监听state的变化,实现了这两者。
如果只是使用redux实现监听state,在子组件中:
componentDidMount() {
this.context.store.subscribe(this.context.store.getState())
}
参考:https://juejin.im/post/5b627b5be51d4534c34a62fa