Redux 的简单总结
文章目录
Redux
https://redux.js.org/
https://cn.redux.js.org/
store.getState()https://redux.js.org/api-reference/store#getstate
这个函数返回获取最新的state,它不会被外界触发。
store.subscribe(listener)https://redux.js.org/api-reference/store#subscribe-listener
这个函数返回一个函数unsubscribe。
这个函数执行后,只要store的state发生任何改变,函数listener就会被执行。直到函数unsubscribe被调用。
目前,官方没有提供监控state部分改变的方法。
store.dispatch(action)https://redux.js.org/api-reference/store#dispatch-action
这个函数执行后,在state中被action提及的成员会被替换。
这个函数返回action。
react-redux
https://github.com/reduxjs/react-redux
https://segmentfault.com/a/1190000015042646
// react-redux
connect([mapStateToProps], [mapDispatchToProps], [mergeProps], [options])
mapStateToProps(state)
mapDispatchToProps(dispatch, ownProps)函数mapStateToProps(state, ownProps)
这个函数能够将state(或其成员)作为 props 绑定到组件上。ownProps是组件本身的 props。
一旦 props 改变,组件就会重新渲染。
函数mapDispatchToProps(state, ownProps)
这个函数提供dispatch,使得组件可以创建用于改变state的props(函数)。ownProps是组件本身的 props。
这个函数与store.dispatch相比,唯一的好处是:假如组件定义不在入口文件(如index.js)中,这种方法可以免于import入口文件中的全局store。所以,这个函数其实并无多大意义。
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论