我有一个动作QUEUE_SWITCH_TO_BACKGROUND_LOCATION_SERVICES
,它更新了redux状态locationServices.pendingModeSwitch
。然后,我在连接的组件didComponentUpdate
中有了一些逻辑,可以接this而至,然后通过调用动作创建者processPendingServiceModeSwitch
来触发redux简化动作。
我遇到的问题是,尽管redux状态已通过初始QUEUE_SWITCH_TO_BACKGROUND_LOCATION_SERVICES
操作成功更新,但它不会导致组件的mapStateToProps
函数调用,这意味着{{1} }不会被调用,因此动作创建者didComponentupdate
也不会被调用。直到几秒钟后发生后续的(不相关的)动作processPendingServiceModeSwitch
才解决。此时,新动作触发了SOCKET_DISCONNECTED
,以及componentDidUpdate和我的mapStateToProps
动作创建者最终被调用。
如果我删除了对动作processPendingServiceModeSwitch
的调用,那么SOCKET_DISCONNECTED
不会被调用,我的didComponentUpdate
也不会被调用。
我在processPendingServiceModeSwitch
函数的选项中重写了areStatesEqual
函数,以检查我是否在做一些愚蠢的事情,例如改变redux状态,但我什至没有发现它被调用,甚至尽管redux日志记录中间件清楚地显示了状态已更改。
我的减速器看起来像这样:
connect()
我的mapStateToProps看起来像这样:
...
case QUEUE_SWITCH_TO_BACKGROUND_LOCATION_SERVICES: {
return {
...state,
pendingModeSwitch: state.locationMode !== "background" ? "background" : state.pendingModeSwitch
};
}
...
我这样连接:
const mapStateToProps = (state: RootState) => {
console.log("location-services:mapStateToProps", state.locationServices, moment().format("HH:mm:ss.SSS"));
return {
locationMode: state.locationServices.locationMode,
pendingModeSwitch: state.locationServices.pendingModeSwitch // this is not being called until the subsequent unrelated action finally causes the didComponentUpdate to fire.
}
}
连接的组件具有const connector = connect(mapStateToProps, dispatchActions, undefined, {
areStatesEqual: (nextState: RootState, prevState: RootState) => {
console.log("react-redux:areStatesEqual: ARE REDUX STATES EQUAL???", nextState === prevState)
return nextState === prevState;
}
})
:
componentDidUpdate
console.logs如下: