首页 > react redux如何区分不同的reducer ?

react redux如何区分不同的reducer ?

代码里面有fetch action负责异步请求数据,toast action负责通知全局信息,现在fetch和toast使用的是两个不同的reducer,每次fetch action或者toast action更新后都会通过react-redux的connect传递到app,数据结构是这样的:

{
    fetchData: {},
    toastInfo: {},
    routing: {}
}

问题是,怎么在app方确定此次更新的action是fetch还是toast?


如果某个组件只关心fetchData的变化,就在componentShouldUpdate里判断

shouldComponentUpdate(nextProps, nextState){
    return !equal(newProps.reducer.fetchData, this.props.reducer.fetchData);
}

equal(a,b) 判断两个值是否相同(不是相等)

react 插件包里 有个叫 shallowCompare 的 equal 实现。


可以通过componentWillReceiveProps参数和当前props比较来判断,很多redux例子也是这么写的。但个人不建议

【热门文章】
【热门文章】