redux – 如何将参数传递给结果createSelector的函数(重新选择)

我的redux商店有两个独立的片A和B,

我需要一个记忆选择器,它返回从B派生的东西,只有当A改变时.

例如

const getSliceA = (state) => state.A

export const getSliceB = createSelector(
 getSliceA,
 (a) => {  return MyDerive(state.B) }
) 

我的问题是如何将状态或state.B发送到resultFunc.

《redux – 如何将参数传递给结果createSelector的函数(重新选择)》

最佳答案

const compareBySliceA = (prevSate: RootState, newState: RootState) => {
    // This is just an example you can compare inner of Slice A
    if (newState.SliceA === prevState.SliceB) {
        return true
    }   
    return false;
};

const getDerivedSliceB (state: RootState): List<any> => 
    state.SliceB.filter(ElementB => ElementB.visible) 


const createComparatorSelector = createSelectorCreator(
  defaultMemoize,
  compareBySliceA,
);


 export const myDeepSelector = createComparatorSelector(
  (state: RootState) => state,
  (state: RootState): List<any> => getDerivedSliceB(state),
);

compareBySliceA和getDerivedSliceB都需要公共父级,即上面的示例State是SliceA和SliceB的parnet.

点赞