NGRX:如何从其他选择器中调用工厂选择器
NGRX: how to call factory selectors from within other selectors
自版本 11 中的 NGRX deprecated selectors with props 以来,使用属性的预期方法是创建工厂选择器,
- 如何嵌套选择器,或从另一个选择器中调用一个并在它们之间传递状态?
更改之前,使用以下两个选择器
export const selector1 = createSelector(
state,
( state: FormState, props: {id: string} ) => {
// Return items whose parent match the given id
return state.items.filter( item => item.parentId === props.id);
}
);
export const selector2 = createSelector(
state
( state, FormState, props: { id: string} ) => {
return state.notes.filter( note => note.parentId === props.id);
}
)
您可以通过以下方式从另一个选择器调用其中一个选择器
export const selector3 = createSelector(
state,
( state: FormState, props: {id: string} ) => {
// get notes by using an existing selector and passing the state & properties
const notes = selector2({ storeName: state}, props)
// do some more logic based on the nested call to a selector
...
}
);
现在工厂选择器是处理属性时的预期格式,现在选择器如下所示
export const selector1 = (id: string) => createSelector(
state,
( state: FormState ) => {
// Return items whose parent match the given id
return state.items.filter( item => item.parentId === id);
}
);
export const selector2 = (id: string) => createSelector(
state
( state, FormState ) => {
return state.notes.filter( note => note.parentId === id);
}
)
- 给定工厂选择器,有没有办法从
selector1
中调用 selector2
- 如果是这样,状态是如何传递给嵌套选择器的
例如
export const selector3 = (id: string) => createSelector(
state,
( state: FormState ) => {
// how is the `state` passed to the nested selector call below?
const notes = selector2( id)
}
);
谢谢。
createSelector
函数可以接收其他选择器作为参数。
所以你可以这样做:
export const selector3 = (id: string) => createSelector(
state,
select2(id),
(state: FormState, filteredNotes ) => {
const notes = filteredNotes;
}
);
自版本 11 中的 NGRX deprecated selectors with props 以来,使用属性的预期方法是创建工厂选择器,
- 如何嵌套选择器,或从另一个选择器中调用一个并在它们之间传递状态?
更改之前,使用以下两个选择器
export const selector1 = createSelector(
state,
( state: FormState, props: {id: string} ) => {
// Return items whose parent match the given id
return state.items.filter( item => item.parentId === props.id);
}
);
export const selector2 = createSelector(
state
( state, FormState, props: { id: string} ) => {
return state.notes.filter( note => note.parentId === props.id);
}
)
您可以通过以下方式从另一个选择器调用其中一个选择器
export const selector3 = createSelector(
state,
( state: FormState, props: {id: string} ) => {
// get notes by using an existing selector and passing the state & properties
const notes = selector2({ storeName: state}, props)
// do some more logic based on the nested call to a selector
...
}
);
现在工厂选择器是处理属性时的预期格式,现在选择器如下所示
export const selector1 = (id: string) => createSelector(
state,
( state: FormState ) => {
// Return items whose parent match the given id
return state.items.filter( item => item.parentId === id);
}
);
export const selector2 = (id: string) => createSelector(
state
( state, FormState ) => {
return state.notes.filter( note => note.parentId === id);
}
)
- 给定工厂选择器,有没有办法从
selector1
中调用 - 如果是这样,状态是如何传递给嵌套选择器的
selector2
例如
export const selector3 = (id: string) => createSelector(
state,
( state: FormState ) => {
// how is the `state` passed to the nested selector call below?
const notes = selector2( id)
}
);
谢谢。
createSelector
函数可以接收其他选择器作为参数。
所以你可以这样做:
export const selector3 = (id: string) => createSelector(
state,
select2(id),
(state: FormState, filteredNotes ) => {
const notes = filteredNotes;
}
);