So, I’m still new to redux toolkit and I’m likely doing some of this wrong. In any case, I’m curious how to extract my “selectors” from my store/slice in redux.
Let’s say I have the following slice:
const mySlice = creatSlice({
name: 'my-slice',
initialState: {
stuff: {
primary: 'Things',
secondary: 'Other Things',
},
},
reducers: (create) => ({
getStuff: create.asyncThunk(
() => getSomeDataAsynchronously(),
{ ...justAssumeTheseUpdateStateWithData },
),
}),
// this is the part I'm most interested in understanding better
// I'm not even sure I'm doing it right. Is this how to handle
// using arguments, or do I need to curry or something?
selectors: {
selectStuff: (state) => state.stuff,
selectSpecificStuff: (state, key) => state[key] ?? 'not found',
}
});
const store = configureStore({
reducer: mySlice.reducer
});
So here’s where I’m most confused. Let’s assume I’ve injected that store in my Provider
, and I’m on a child react component:
import { useSelector } from 'react-redux';
const MyComponent = () => {
const selectSpecificStuff = useSelector(/* how to access from my slice? */);
return <>{selectSpecificStuff('primary')}</>;
}
Is this even possible? I can’t seem to find anything about it in the docs, which makes me wonder what the point of the selectors property is, if you can’t access it.
Any assistance is appreciated.