[Docs] createSlice + selectors with Parameters #4562
Replies: 2 comments
-
|
Your slice selectors get wrapped so they can be called with the root state, depending on how you get them from your slice: // .selectors uses rootState[slice.name] (technically slice.reducerPath, which defaults to slice.name)
counterSlice.selectors.selectTimes({ counter: { value: 1 } }, 2)
// getSelectors() with no arguments expects slice state only
counterSlice.getSelectors().selectTimes({ value: 1 }, 2)
// explicit input selector will be called to get slice state
counterSlice.getSelectors((rootState: RootState) => rootState.foo).selectTimes({ foo: { value: 1 } }, 2)They will still only ever be called with the slice state. (rootState, ...args) => yourSliceSelector(selectSliceState(rootState), ...args) |
Beta Was this translation helpful? Give feedback.
-
|
I understand that, my question was more about running the selector in the callback. But I'm getting that, that is not possible (I think obviously because it is technically a react hook and you can't run hooks conditionally / in callbacks) So I think the most technically correct answer would be to Either way, 🙏🏻 and apologies for delayed response. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
The documentation gives the following example:
What would be the correct way to use
selectTimesand provide atimesvalue of2?To give a little more context, let's say I have a context which exposes a function with a parameter of
multiplier. The param is then used to select a new value from the slice using theselectTimes.Something like:
So I think the answer is something along the lines of "you cannot use a selector (ie. a react hook) like that in a function call". But then is the only way to use
selectTimesthe following:If that is the only way to use
selectTimeswith a value of2, that's fine it just feels a little awkward. Because unless I'm missing something (which is why I'm posting here), thestatebeing passed to thecounterSlice.selectors.selectTimesis the global state but somehow that becomes the slice state only when theselectTimes: (state, times = 1): number =>function is invoked.Last note: My actual use case is using the entity adapter for a slice and trying to figure out the correct relationship between the slice selectors and the selectors provided by the entity adapter.
Beta Was this translation helpful? Give feedback.
All reactions