我的商店有一个非常简单的状态:
const state = {
records: [1,2,3],
};
我有一个记录选择器:
export const getRecords = createSelector(getState, (state: State) => state.records));
我现在想要的是有单独的选择器来按索引获取每条记录。
为此,我想用以下方式创建一个带有 props 的通用选择器:
export const getRecordByIndex = createSelector(
getRecords,
(state: State, { index }) => state.records[index]),
);
然后创建几个特定的选择器 e。 G。:
export const getFirstRecord = createSelector(
getRecordByIndex(/* somehow pass index = 0 to this selector */),
(firstRecord) => firstRecord),
);
但我没有发现任何提及当我们在 createSelector 方法中使用它们时如何使用 props 将参数传递给选择器。是否可以?
来自这篇博文:https://timdeschryver.dev/blog/parameterized-selectors https://timdeschryver.dev/blog/parameterized-selectors
从 NgRx 6.1 开始,选择器还接受额外的 props 参数。哪个
意味着您现在可以定义一个选择器,如下所示:
export const getCount = createSelector(
getCounterValue,
(counter, props) => counter * props.multiply
);
this.counter = this.store.pipe(
select(fromRoot.getCount, { multiply: 2 })
);
啊......但是重读你的问题,你会问如何构建另一个使用这个选择器的选择器?上面链接的文章建议构建一个工厂函数。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)