[英]React/Redux Component not re-rendering
I am having a problem with a react component not re-rendering even though I see the state being properly updated. 我遇到问题,即使看到状态已正确更新,React组件也无法重新渲染。 I am using redux, I have read state mutation is often the issue/cause of this I just dont think I am doing that.
我正在使用redux,我读过状态突变通常是导致此问题的原因,我只是认为自己没有这样做。 The value I am updating is nested 3 levels deep within the state.
我要更新的值在状态内嵌套了3个级别。
Here is the code: 这是代码:
import { RECEIVE_CURRENT_SCAN_RESULT } from '../constants';
const initialState = {
currentScanResult: {info:{}, results:[]},
};
[RECEIVE_CURRENT_SCAN_RESULT]: (state, payload) =>
Object.assign({}, state, {
currentScanResult: Object.assign(state.currentScanResult, payload)
}),
export function createReducer(initialState, reducerMap) {
return (state = initialState, action) => {
const reducer = reducerMap[action.type];
return reducer
? reducer(state, action.payload)
: state;
}
}
In Object.assign
the first argument is the "target" object, 在
Object.assign
,第一个参数是“目标”对象,
so this line 所以这条线
currentScanResult: Object.assign(state.currentScanResult, payload)
is still mutating the currentScanResult
property on the state. 仍在改变状态的
currentScanResult
属性。
Use an empty object as first argument in Object.assign
, like you did on the first level, so the whole thing becomes: 像在第一级上一样,将空对象用作
Object.assign
第一个参数,因此整个过程变为:
// empty object as target here
Object.assign({}, state, {
// empty object as target here as well
currentScanResult: Object.assign({}, state.currentScanResult, payload)
})
and that should at least test if the the problem is a problem of mutation or if there is something else that needs fixing. 至少应该测试该问题是否是突变问题,或者是否还有其他需要解决的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.