3

I have a large selector that loops over an array and calls a selector for each item in the array. Is there any easy way to manage this?

It looks kind of like this:

const memoizedGetPatientSymptomSeries = createSelector(
    state => getCurrentPatientId(state),
    state => displayPrefSelectors.getSymptomsToView(state), 
    (pid, selectedSymptoms) => {
        selectedSymptoms.forEach( symptom => {
            const symptomInfo = getSymptomInfoSelector(state, symptom.id)
        }
    }
)

Does anyone know how I can do this?

My only thought would be that I have to copy and paste the getSymptomInfo selector into the loop itself.

Swish
  • 359
  • 4
  • 14
  • What are you trying to achieve? A memoized instance for each call to `getSymptomInfoSelector` per _symptom_? – chautelly Oct 01 '19 at 19:38
  • I have a selector that uses other selectors inside of a loop. For that to work with reselect, you need to be able to pass in state, but that'd defeat the entire purpose. I found a way to do it on github. i'll post the answer here – Swish Oct 02 '19 at 00:16

1 Answers1

1

I found the answer here

All it takes is creating a memoized selector that return a function which takes in the arguments you'd pass in aside from the state. Then you add that selector factory function as an argument in the original memoized selector.

nmilcoff
  • 1,084
  • 5
  • 20
Swish
  • 359
  • 4
  • 14