NGRX - 将选择器与 props 结合起来

bar*_*zek 4 ngrx ngrx-store

当其中一个减速器需要道具时,如何组合减速器?

我有以下型号:

interface Device {
  id: string;
  data: IDeviceData;
}
Run Code Online (Sandbox Code Playgroud)

和 DeviceReducer 如下所示:

import { EntityState, EntityAdapter, createEntityAdapter } from '@ngrx/entity';
import { Device } from '../model/device';
import { SubnetBrowserApiActions } from 'src/app/explorer/actions';

export interface State extends EntityState<Device> { }

export const adapter: EntityAdapter<Device> = createEntityAdapter<Device>();

export const initialState: State = adapter.getInitialState();

export function reducer(
  state = initialState,
  action:
    | SubnetBrowserApiActions.SubnetBrowserApiActionsUnion
): State {
  switch (action.type) {
    case SubnetBrowserApiActions.SubnetBrowserApiActionTypes.LoadEntriesSucces: {
      return adapter.upsertMany(action.payload.entries, state);
    }

    default: {
      return state;
    }
  }
}

const {
  selectAll,
} = adapter.getSelectors();

export const getAllDevices = selectAll;
Run Code Online (Sandbox Code Playgroud)

在我的另一个减速器中,当我想使用 ids 数组选择设备时,我使用以下代码:

export const getVisibleDrives = createSelector(
  [fromRoot.getAllDevices, getVisibleDrivesSerialNumbers],
  (devices, visibleDevicesIds) =>
    devices.filter((device) => onlineDevicesIds.includes(device.serialNumber)),
);
Run Code Online (Sandbox Code Playgroud)

这段代码非常重复,所以我想添加添加参数化选择器,它将仅返回这些驱动器,这些驱动器在我作为 prop 传递的数组中具有 id。我尝试做的事情如下所示:

DeviceReduced 中的附加选择器

export const getDrivesWithIds = (ids: string[]) => createSelector(
  getAllDevices,
  devices => devices.filter(device => ids.includes(device.id))
);
Run Code Online (Sandbox Code Playgroud)

然后按以下方式组合它们:

export const getVisibleDrives = createSelector(
  getVisibleDrivesSerialNumbers,
  (ids) => fromRoot.getDrivesWithIds
);
Run Code Online (Sandbox Code Playgroud)

这里的问题是这个选择器的返回类型是

(ids: string[]) => MemoizedSelector<State, Device[]>

这使得我无法使用这个选择器做任何有用的事情。作为一个例子,我想按关键字过滤此列表,但我无法filter对其使用方法:

用法示例

export const getFilteredVisibleDrives = createSelector(
  [getVisibleDrives, getKeywordFilterValue],
  (visibleDrives, keywordFilter) => {
    return visibleDrives
      .filter(drive => // in this line there is an error: Property 'filter' does not exist on type '(ids: string[]) => MemoizedSelector<State, Device[]>'
        drive.ipAddress.toLowerCase().includes(keywordFilter.toLowerCase()) ||
        drive.type.toLowerCase().includes(keywordFilter.toLowerCase()) ||
        drive.userText.toLowerCase().includes(keywordFilter.toLowerCase())
      );
  },
);
Run Code Online (Sandbox Code Playgroud)

tim*_*ver 7

有关更多信息,请参阅我的文章NgRx:参数化选择器。

更新:NGRx v13+

带 props 的选择器已被弃用,请使用选择器工厂代替:

选择器:

export const getCount = (props: {id: number, multiply:number}) =>   
  createSelector(     
    (state) => state.counter[props.id],     
    (counter) => counter * props.multiply
  );
Run Code Online (Sandbox Code Playgroud)

成分:

this.counter2 = this.store.pipe(
  select(fromRoot.getCount({ id: 'counter2', multiply: 2 })
);   
this.counter4 = this.store.pipe(
  select(fromRoot.getCount({ id: 'counter4', multiply: 4 })
);
Run Code Online (Sandbox Code Playgroud)

已弃用

选择器:

export const getCount = () =>   
  createSelector(     
    (state, props) => state.counter[props.id],     
    (counter, props) => counter * props.multiply
  );
Run Code Online (Sandbox Code Playgroud)

成分:

this.counter2 = this.store.pipe(
  select(fromRoot.getCount(), { id: 'counter2', multiply: 2 })
);   
this.counter4 = this.store.pipe(
  select(fromRoot.getCount(), { id: 'counter4', multiply: 4 })
);
Run Code Online (Sandbox Code Playgroud)