如何在createSelector方法中使用现有的参数化选择器

时间:2019-02-08 09:26:59

标签: typescript redux ngrx

我想从参数化选择器中创建一个选择器。我的理解是,只有在更新特定项目时才会发出此消息。

const getFeatureState = createFeatureSelector<FragmentsHierarchyState>(MY_FEATURE);

const getItem = createSelector(
    getFeatureState,
    (state: FeatureState, props: { itemId: string }) => {
        return state.items[props.itemId];
    }
);

const getItemValue = createSelector(
    getItem, // <<< How to pass props here?
    (state: ItemState) => return state.x + state + y
);

我不想执行以下操作,因为我不想每次功能状态更新时都发出新值:

const getItemValue = createSelector(
    getFeatureState,
    (state: FeatureState, props: { itemId: string }) => {
        return state.items[props.itemId].x + state.items[props.itemId].y;
    }
);

1 个答案:

答案 0 :(得分:0)

getFeatureState选择器也应该正常接收道具...

如果不是这种情况,可以使用工厂功能

const getItem = ({ itemId: string }) => createSelector(
    getFeatureState,
    (state: FeatureState) => {
        return state.items[itemId];
    }
);