7 ms·
Redux has the problem that (a) the read-only reducer concept is totally at odds with how a bunch of JS builtins work and so conceptually hard to understand for
by crooked-v 8y ago
Redux has the problem that (a) the read-only reducer concept is totally at odds with how a bunch of JS builtins work and so conceptually hard to understand for a newbie, (b) it has just so much boilerplate for "best practices" for even the simplest use case, and (c) it's not immediately clear what the benefits of wrapping your head around all that are.
For me, it took me a while of working on a web app with derived data everywhere (~15 KB raw JSON that gets updated with diffs and recombobulated into different output formats and different intersections of data with live updates), and endless debugging of data transformations by stepping back and forth through the reducer history, before I really "got" it.
On the note of (b), I really like the redux-actions project (https://redux-actions.js.org https://redux-actions.js.org) for the way its helpers that get rid of most of that. A simple example:
const setValueA = createAction('SET_VALUE_A');
const setValueB = createAction('SET_VALUE_B');
const setValueAB = createAction('SET_VALUE_AB', (a, b) => ({ a, b }));
export const reducer = combineReducers({
valueA: handleActions({
[setValueA]: (state, action) => action.payload
[setValueAB]: (state, action) => action.payload.a
}, null),
valueB: handleActions({
[setValueB]: (state, action) => action.payload
[setValueAB]: (state, action) => action.payload.b
}, null)
});
- acemarke 8y agoWe've actually got a new `redux-starter-kit` package which includes some reducer utilities similar to `redux-actions`, but using the Immer library internally. That allows you to write simpler immutable updates that look like "mutation". There's also some helpers for common use cases like simplifying store setup, and generating entire "slices" of state at once (including generating action creators and action types automatically). I put together an example of using this the other day: https://www.reddit.com/r/reactjs/comments/a0b1sa/frustrated_with_redux_so_i_created_abredux_vs/eagqit6/ https://www.reddit.com/r/reactjs/comments/a0b1sa/frustrated_... Please try it out and let us know what you think! https://github.com/reduxjs/redux-starter-kit https://github.com/reduxjs/redux-starter-kit
- crooked-v 8y agocreateSlice is an interesting approach, though it feels like it's missing the use case of being able to set multiple keys in the same reducer from one action, as with my valueAB example above. It might be worth putting some thought into how to make a variant of it that can produce multiple reducers (one per key to be affected), not just one.
- acemarke 8y agoYeah, we had one user file an issue with some suggested improvements: https://github.com/reduxjs/redux-starter-kit/issues/41 https://github.com/reduxjs/redux-starter-kit/issues/41 Haven't had time to dig into this further, though. Think you could leave a comment there with your suggestions and that example?