5
export const mySelector = createSelector(
    [selectorA, selectorB], // but I want selectorB or selectorC to be chosen here using logic
    (foo, bar) => {
        // ...
    }
);

I want to conditionally use selectorB or selectorC when creating this selector based on the state in the store (i.e. the value returned by another selector).

How can I do this?

Ben Aston
  • 53,718
  • 65
  • 205
  • 331

2 Answers2

10

Just to point out that the approach suggested in the accepted answer would evaluate both selectorB and selectorC on each selectorCorB call. Which is not probably the desired behaviour.

An actual conditional call might be implemented like this:

export const selectorCorB = createSelector(
    selectorStateX
    (...arguments) => arguments,
    (x, arguments) => x ? selectorB(...arguments) : selectorC(...arguments)
);
Andrea Carraro
  • 9,731
  • 5
  • 33
  • 57
3

then make a selectorCorB composing selectorB, selectorC , selectorX

selectorX is the selector of that variable x from store which decides either selectorB or selectorC

const selectorCorB = createSelector(
  selectorB,
  selectorC,
  selectorStateX,
  (b, c, x) => x? b: c
) 

mySelector = createSelector(
    [selectorA, selectorCorB], // will be A AND B OR C depending on x
    (foo, cOrb) => {
        // ...
    }
);
Shishir Arora
  • 5,521
  • 4
  • 30
  • 35