Currently, I have all my adapters defined in a centralized file and exporting them from there. In my slice, I export a selectors
object that includes selectors generated by my entityAdapter
. Since the entityState is nested within the slice state, I've defined the selectors as follows:
const entitySelectors = myAdapter.getSelectors<RootState>((state) => state.sliceState.entities)
I want to use these selectors in the reducers of the same slice they are exported from, so I don't duplicate logic already provided by the adapter. Is there an efficient way to achieve this without defining two versions of the selectors (one for RootState and one for SliceState)?
One approach could be:
const sliceStateSelectors = myAdapter.getSelectors<SliceState>((state) => state.entities);
const rootStateSelectors = myAdapter.getSelectors<RootState>((state) => state.sliceState.entities);
export const selectors = {
...rootStateSelectors,
// other selectors
}
This setup allows me to use sliceState selectors in my reducers, but it may not be ideal in terms of clarity and avoiding repetition.