【发布时间】:2020-09-06 06:47:08
【问题描述】:
在我的 React Redux 应用程序中,setInterval() 不断调用操作创建者 this.props.getLatestNews() 来查询 REST API 端点。在获取 API 响应(对象数组)后,它将以响应数组作为有效负载分派一个动作。
反应组件
class Newsfeed extends Component {
constructor(props) {
super(props);
this.state = { ... }
}
componentWillMount() {
this.updateTimer = setInterval(() => { this.props.getLatestNews() }, 1000);
}
componentWillUnmount() {
clearInterval( this.updateTimer );
}
shouldComponentUpdate(nextProps, nextState) {
// Should we do a deep compare of nextProps & this.props,
// shallow compare of nextState & thisState?
}
render() {
return ( ... )
}
}
const mapStateToProps = (state) => ({
newsfeed: state.newsfeed
});
const mapDispatchToProps = (dispatch) => {
return {
getLatestNews: () => dispatch(getLatestNews())
}
};
export default connect(mapStateToProps, mapDispatchToProps)(Newsfeed);
在reducer中,目前总是更新部分状态,不管有没有变化
减速器
export default function newsfeedReducer(state=initialState, action) {
switch (action.type) {
case NEWSFEED_RECEIVED:
// Or should we do the deep compare of `action.payload` with `state.items` here?
return { ...state, items: action.payload }
default:
return state
}
}
Root Reducer
...
const rootReducer = combineReducers({
newsfeed: newsfeedReducer
});
...
大多数时候,当收到 API 结果时,Redux 存储中没有不存在的新项目。如果reducer仍然用API结果替换了store的这一部分,由于新对象引用props,连接的组件将重新渲染,除非我们在shouldComponentUpdate()内部进行深度比较。
为了避免对 React 组件进行不必要的重新渲染,我正在考虑在 shouldComponentMount() 函数中对当前和下一个道具进行深度比较,或者对 action.payload 与 @ 进行深度比较987654330@ 应该替换它。
进行深度比较的推荐位置在哪里?或者这根本没有必要?
谢谢!
【问题讨论】:
-
什么是
shouldComponentMount?你的意思是shouldComponentUpdate?那将是您进行比较的地方。 -
@jmargolisvt 抱歉打错了,应该是
shouldComponentUpdate。 -
您的意思是
mapStateToProps = (state) => ({ newsfeed: state.items })?否则state.items更改不会影响此组件。 -
是的,我确实建议您在
newsfeedReducer()中进行深入比较。 -
@hackape 对不起,我忘了包括
NEWSFEED_RECEIVED的减速器被传递到根减速器的事实。更新的问题。
标签: javascript node.js reactjs redux react-redux