-
-
Notifications
You must be signed in to change notification settings - Fork 848
New issue
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
Introduce create(Immer)Reducer
utility?
#31
Comments
createImmerReducer
/ createImmerReducers
utiltiescreate(Immer)Reducer
utility?
To handle the initial state, I'd rather do that: export function createImmerReducer (initial, fn) {
return (state = initial, action) => immer(state, draft => fn(draft, action))
} That you can use like that: const INITIAL_STATE = { foo: null }
const reducer = createImmerReducer(INITIAL_STATE, (draft, action) => {
switch (action.type) {
case 'SET_FOO':
draft.foo = action.foo
}
})
export default reducer |
what with the reducers called by main reducer? if I'm not mistaken it would be sufficient to just mark top-level reducer and rest of reducers could be called as normal functions? function helperReducer(draft, action) {
draft.foo = action.foo
}
const reducer = createImmerReducer(INITIAL_STATE, (draft, action) => {
switch (action.type) {
case 'SET_FOO':
helperReducer(draft, action);
} |
Personally, I would just go with import { createReducer } from 'immer' I would also like to see a few other utilities that help with redux interop and reusing as much of the redux ecosystem as possible:
There is above are just some basic implementations of what I mean and probably wrong in 1000 different ways, but I hope you get the idea. You can definitely add a heap of safety and hinting code around them the help people out. Would all this belong in the |
Implemented currying in 0.4.0, see https://github.com/mweststrate/immer#currying
I think Closing for now, I think the above patterns are after all so simple that they can easily be implemented in userland code. Let's have the current implementation settle a bit and wait until the dust is cleared :) |
https://github.com/salvoravida/redux-immer another approach ... Salvo |
the pattern is now typically:
So we could introduce a utility
createImmerReducer(fn: (draftState, action) => {})
which allows writing
The implementation would roughly be:
The text was updated successfully, but these errors were encountered: