2

由于 NGRX在版本 11 中弃用了带有 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);
    }
)
  • 给定工厂选择器,有没有办法selector2从内部调用selector1
  • 如果是这样,状态如何传递给嵌套选择器

例如

export const selector3 = (id: string) => createSelector(
   state,
   ( state: FormState ) => {
       // how is the `state` passed to the nested selector call below? 
       const notes = selector2( id)
   }
);

谢谢你。

4

1 回答 1

1

createSelector函数可以接收其他选择器作为参数。

所以你可以这样做:

export const selector3 = (id: string) => createSelector(
   state,
   select2(id),
   (state: FormState, filteredNotes ) => {

       const notes = filteredNotes;
   }
);
于 2021-12-17T06:52:08.573 回答