存在问题,mapDispatchToProps
中的状态似乎正在更新,但是即使...,组件也不会重新呈现以反映这些更改。
mapDispatchToProps
成功地初始化道具并调用mapDispatchToProps
函数)const initialState = {
session: {
...
toUpdate: {success: false, processing: false, message: ''},
...
},
someOtherStateSections: {...},
...
}
const sessionReducer = (state=initialState.session, action) => {
switch (action.type) {
...
case Types.ACTION_TO_HANDLE:
console.log(`REDUCER: ACTION_TO_HANDLE: check that state is being updating with intended values:\n${JSON.stringify(action, null, 2)}`)
return Object.assign({}, state, {toUpdate: {
success: action.success,
processing: action.processing,
message: action.message,
}})
...
}
}
const rootReducer = combineReducers({
session: sessionReducer,
...
})
export default rootReducer;
state
arg。 mapStateToProps
函数中的“正在更新”以反映这些更改。代码段看起来像const mapStateToProps = (state, ownProps) => {
console.log(`PickupLocationAnchorFormContainer: mapStateToProps: ${JSON.stringify(state.session.pickupLocationAnchorChangeStatus, null, 2)}`)
return {
toUpdate: state.session.toUpdate,
ownProps: ownProps,
}
}
const mapDispatchToProps = (dispatch,) => {
return {
someThunk: (some, inputs) => {
dispatch(someThunk(some, inputs))
},
}
}
const MyComponentContainer = connect(
mapStateToProps,
mapDispatchToProps
)(MyComponent)
export default MyComponentContainer
MyComponent
似乎还没有更新其this.props.toUpdate
来处理更新后的redux存储值。
toUpdate.success
从false
设置为true
),使我怀疑问题是否与浅层问题有关。相等性检查(请参阅https://redux.js.org/faq/reactredux#why-isnt-my-component-re-rendering-or-my-mapstatetoprops-running)(尽管到目前为止尚无法解决该错误,所以我知道吗,对吧?)任何进一步的调试建议或推荐的解决方案将不胜感激。
答案 0 :(得分:0)
尝试在reducer中编写此代码:
const sessionReducer = (state=initialState, action) => {
switch (action.type) {
...
case Types.ACTION_TO_HANDLE:
return Object.assign({}, state, {
...state,
session: {
...state.session,
toUpdate: {
success: action.success,
processing: action.processing,
message: action.message,
},
}
})
}
}