react-redux的连接在分派后续操作之前不会为某个同步操作调用mapStateToProps

时间:2020-02-22 04:37:13

标签: react-native redux react-redux

我有一个动作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如下:

enter image description here

0 个答案:

没有答案