重新选择createStructuredSelector未更新

时间:2019-03-19 12:02:41

标签: reactjs redux reselect

我有一个redux连接的组件,它像这样使用createStructuredSelector:

const mapStateToProps = state =>
    createStructuredSelector({
        fruits: selectShopFruits(),
    });

我有很多选择器,它们彼此组成(从上到下都是线性的 1-> 2-> 3-> 4 ):

const selectDomain = () => state => {
    console.info('(1)')
    return state ? state.shops : {};
};

export const selectShop = () =>
    createSelector(
        selectDomain(),
        shops => {
            console.info('(2)');
            return shops.shopList.find(shop => shop.selected)
        }
    );

export const selectShopInventory = () =>
    createSelector(
        selectShop(),
        shop => {
            console.info('(3)')
            return shop.inventory
        }
    );

export const selectShopFruits = () =>
    createSelector(
        selectShopInventory(),
        inventory => {
            console.info('(4)')
            return inventory && inventory.food ? inventory.food.fruits: []
        }
    );

我的问题是,在更新 inventory.food.fruit s时没有调用selectShopFruits。我的reducer正在更新我的inventoy.food.fruit,但没有调用选择器。

我的输出看起来像这样:

(1)
(2)
(3)
(4)
 UPDATING INVENTORY IN REDUCER!!!!!!!!!!!!!!!!!!
(1)
(2)

有趣的是,如果我这样编写mapStateToProps:

const mapStateToProps = state => {
    return {
        fruits: selectShopFruits()(state),
    };
};

确实有效。

我的输出看起来像这样:

(1)
(2)
(3)
(4)
 UPDATING INVENTORY IN REDUCER!!!!!!!!!!!!!!!!!!
(1)
(2)
(3)
(4)

为什么我的createStructuredSelector无法正常工作?某种“深度等于”失败了吗?

0 个答案:

没有答案