At the moment, im defining all my adapters in an adapters file and exporting them from there. Within my slice, i export a selectors object within which i include the selectors generated by my entityAdapter. Since my entityState is nested within the slice state, i've defined the selectors like so:
const entitySelectors = myAdapter.getSelectors<RootState>((state) => state.sliceState.entities)
I want to use these selectors in the reducers (which have access to slice state) of the same slice they are exported from so that i'm not writing logic that the adapter already has for me. Is there an elegant way to do this without having to define two versions of the same selectors (one for RootState, and one for SliceState)?
I could just have:
const sliceStateSelectors = myAdapter.getSelectors<SliceState>((state) => state.entities);
const rootStateSelectors = myAdapter.getSelectors<RootState>((state) => state.sliceState.entities);
export const selectors = {
...rootStateSelectors,
// other selectors
}
and use sliceState selectors in my reducers, but it doesn't seem desirable from a clarity/repetition standpoint.
A Redux selector does typically expect the entire Redux root state object as an argument, but a slice is only a part of that state. So, yes, if you want to use selectors inside of reducers, you'd need to create the alternate versions that can accept just the slice state.
That said: I honestly would recommend against trying to use selectors inside of a slice's reducers. There's not really any benefit here. It's easy enough to do state.entities[someId] instead of selectById(), etc, and the slice's reducer logic is in control of how those items are being stored anyway.