Hiran
Hiran

Reputation: 698

How to pass parameters to the result Function of a createSelector (reselect)

My redux store has two independent slices A and B,

I need a memorized selector which returns something derived from B, only if A changes.

For example

const getSliceA = (state) => state.A

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

My problem is how to send the state or state.B to the resultFunc.

enter image description here

Upvotes: 4

Views: 596

Answers (1)

Hiran
Hiran

Reputation: 698

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),
);

Both compareBySliceA and getDerivedSliceB needs the common parent, that is in above example State is the parnet of SliceA and SliceB.

Upvotes: 1

Related Questions