如何在选择器中获得不同的状态或如何在选择器内部使用选择器

How to get a different state in your selector or how to use a selector inside of a selector

我有以下选择器

const getLinkFeatureState = createFeatureSelector<ILinkReducer>('link');

export const getLinkIsLinkDtoPricesDeliveryTimesSome = (index: number) => createSelector(
  getLinkFeatureState,
  state => state.linkDto.prices[index].deliveryTimes.some(d => d.iso == state.???)
)

现在我想使用我的另一个减速器的状态

export interface ISelectedLanguageReducer {
  language: ILanguage;
  isLanguageSuccess: boolean;
}
const selectedLanguageInitial: ISelectedLanguageReducer = {
  language: {
    country: '',
    value: '',
    selected: false
  },
  isLanguageSuccess: false
}

使用以下选择器

const getSelectedLanguageFeatureState = createFeatureSelector<ISelectedLanguageReducer>('selectedLanguage');
export const getSelectedLanguageLanguage = createSelector(
  getSelectedLanguageFeatureState,
  state => state.language
);

有没有办法在 'link' 选择器中获取所选语言或在选择器中使用选择器?

您可以通过直接在 createSelector 函数中添加另一个选择器来实现,如下所示:

export const getLinkIsLinkDtoPricesDeliveryTimesSome = (index: number) =>
    createSelector(getLinkFeatureState, getSelectedLanguageLanguage, (state, selectedLang) =>
        // >>> you can use the selectedLang here
        state.linkDto.prices[index].deliveryTimes.some((d) => d.iso == state.???)
    );

确保在使用前在模块中初始化另一个状态。

您可以在 Using selectors for multiple pieces of state 处检查 createSelector 的重载。