We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
setPropCaseReducer
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I'm sure that this caseReducer
(state: State, action: PayloadAction<T>) => { state.propName = action.payload; };
Is common enough that @reduxjs/toolkit should supply a function abstraction of it
@reduxjs/toolkit
const setPropCaseReducer = <State, Prop extends keyof State>(prop: Prop) => (state: State, action: PayloadAction<State[Prop]>) => { state[propName] = action.payload; }; type UserState = { email: string; }; const initialState: UserState = { email: '' }; const useSlice = createSlice({ name: 'user', initialState, reducers: { setEmail: setPropCaseReducer<UserState>('email') } });
The text was updated successfully, but these errors were encountered:
this pattern is explicitly discouraged - reducers should be modeled as events and modify as many properties as is necessary.
Sorry, something went wrong.
¯\_(ツ)_/¯
Yeah, this is similar to the answer I gave in #2658.
We actively don't want users to be writing a bunch of reducers that are all state.fieldName = action.payload all the time.
state.fieldName = action.payload
That's not to say you should never do this - I've even written a few of these myself over in the Replay codebase.
But it's a pattern we want users to minimize, rather than one we want to encourage.
No branches or pull requests
I'm sure that this caseReducer
Is common enough that
@reduxjs/toolkit
should supply a function abstraction of itThe text was updated successfully, but these errors were encountered: