You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@reduxjs/toolkit has done an amazing job abstracting common functionality to reduce boiler plate. One thing that I feel is missing is that createSlice does not produce base selectors for the root keys of a reducer
So I would like to add selectors to the return object of createSlice
selectors has a key matching each key of initialState that is passed to createSlice. The idea is to simply allow us to reuse a pre-generated selector function the same way actions are generated
// user.slice.tstypeUserState={email: string;}constinitialState: UserState={email: ''};exportconstuserSlice=createSlice({name: 'user',
initialState,reducers: {setEmail: (state,action: PayloadAction<string>)=>{state.email=action.payload;}}});// UserInfo.tsximport{userSlice}from'../user.slice';constUserInfo=()=>{constdispatch=useDispatch();// we should use selectors generated right from the sliceconstemail=useSelector(userSlice.selectors.email);constonChange=(e)=>{// the same way we use actions!dispatch(userSlice.actions.setEmail(e.currentTarget.value);};return(<inputonChange={onChange}value={email}/>);}
The intention of these pre-generated selectors is simply to cover the most common use case of a reducer, one with a flat object where you just want to get the value of each key. No more, no less.
This addition would be incredibly simple to add with very little overhead and provides additional reduction of boilerplate. And while it would not always be used for more complex reducers, I don't feel there would be much harm or overhead to including them regardless
Alternative to always including them:
opt-in to their generation by passing createSlice({ selectors: true });
granularly select which keys get generated createSlice({ selectors: ['email'] });, where selectors: (keyof State)[]
The text was updated successfully, but these errors were encountered:
Yep. We actually discourage having selectors for every single field in a slice. Additionally, there's no point in generating memoized selectors for each field:
Another issue is that these selectors wouldn't know anything about the root state type, which is what a typical selector expects as its first argument. We could hypothetically do something like the entityAdapter.getSelectors(selectTheRightPieceOfData) approach, but at that point there really isn't much benefit over just writing a couple of functions yourself.
@reduxjs/toolkit
has done an amazing job abstracting common functionality to reduce boiler plate. One thing that I feel is missing is thatcreateSlice
does not produce base selectors for the root keys of areducer
So I would like to add
selectors
to the return object ofcreateSlice
selectors
has a key matching each key ofinitialState
that is passed tocreateSlice
. The idea is to simply allow us to reuse a pre-generated selector function the same way actions are generatedThe intention of these pre-generated selectors is simply to cover the most common use case of a reducer, one with a flat object where you just want to get the value of each key. No more, no less.
This addition would be incredibly simple to add with very little overhead and provides additional reduction of boilerplate. And while it would not always be used for more complex reducers, I don't feel there would be much harm or overhead to including them regardless
Alternative to always including them:
createSlice({ selectors: true });
createSlice({ selectors: ['email'] });
, whereselectors: (keyof State)[]
The text was updated successfully, but these errors were encountered: