react 问题集合
1.一处prop更新全局更新
原因:不同组件共用了相同的state,
2.reducer更新时,对比没有变化
组件状态跟新代码
componentDidUpdate(preProps, preState) {
if (this.props.mapMsg.logAry != preProps.mapMsg.logAry &&
this.props.mapMsg.logAry.length != this.state.timeAry.length) {
//初始化成功开始缓冲数据
this.query.current = this.props.mapMsg.logAry.length;
this.props.dispatch(optionApi(api.getMap, this.query, actionTypes.NEW_MAP))
}
}
reducer
export function mapMsg(state = {}, action) {
switch (action.type) {
case actionTypes.ORG_MAP:
return action.data;
case actionTypes.NEW_MAP:
state.logAry.push(...action.data.logAry);
action.data.logAry = state.logAry;
return action.data;
}
return state;
}
原因:在reducer时将原来state的值也进行了修改
解决方案如下
export function mapMsg(state = {}, action) {
switch (action.type) {
case actionTypes.ORG_MAP:
return action.data;
case actionTypes.NEW_MAP:
let newLogAry = [...state.logAry];
newLogAry.push(...action.data.logAry);
action.data.logAry = newLogAry;
return action.data;
}
return state;
}