If anyone reading this wants to learn Redux, just read the getting started guide, or watch the co-creator's video series.[1] It's wonderful. If anyone just wants a TL;DW, here's mine:
1. The state of your application should be a tree. This tree is immutable. We call it a store.
2. Each branch of your state tree is generated by a pure function of the previous state and an action. We call these reducers.
3. The only way to effect change in your state is to dispatch actions. Dispatching action X triggers the root reducer for action X (which triggers the child reducers with action X, etc.), giving you the next state.
4. Subscribe to store changes with listeners.
That's really it. A general roadmap for managing state in your software. Has nothing to do with React. Or with user interfaces. Just a general approach to software state. You can roll your own in a few minutes for your use-case. If your use case happens to be a React UI or C# app, then yeah, reach for React-Redux or Redux.NET respectively.
But please, please don't learn Redux initially by looking at a React app that uses React-Redux. That's hardcore jumping the gun.
[1] https://egghead.io/lessons/react-redux-the-single-immutable-...
That boilerplate can easily take 10x more lines than "real" code in reducers, even in complex applications. Requiring three different files for each reducer, as in the article presented, is completely overkill, and it only makes sense when you're getting paid by the line of code.
However, there are great solutions to avoid that. I'll echo the recommendation of createSlice:
He only has one [0]. Note that the parent directory is "redux", not "reducer" - the actions and actionTypes are separate from the reducers, because there isn't a 1:1 relationship. This separation is because you could, for example, have a second reducer that listens to the same Add action as the one in this example - it doesn't need it's own action or actionType.
[0] https://miro.medium.com/max/1383/1*JsqDyA2Wy_08GnHS6SMDmw.pn...
Actions and ActionTypes are pure noise.
And idk, having multiple reducers responding to the same action is a recipe for trouble.
Now, it's certainly true that _most_ actions will only be handled by a single slice reducer, but having many slice reducers responding to the same action is an incredibly useful pattern:
https://redux.js.org/faq/actions#is-there-always-a-one-to-on...
But even leaving that aside, I still think that this specific "feature" is not worth the amount of boilerplate you need in most Redux apps, so it's not a nice tradeoff IMO :(
I worked in some bigger apps, and before adopting ducks (and later a homebrew version of createSlice) we had some bugs caused by multiple reducers firing.
I also think that it makes Flux/Redux a bit harder to explain
Redux isn't complex, it's complicated - needlessly so.
It takes just one look at its competitors to see this.
It only becomes complex when people trying to show a "cultured" way to write a large redux/react app bring in a ton of containers, controllers, action classes with methods for various auxiliary conveniences, etc. Even the example in redux's own docs is unnecessarily complex.
This is not how you explain things.
For an explanation, i'd use a single file, with 2-3 actions, using strings (or an enum) to denote actions, with a reducer right next to renderer, and a single-function react component.
Then the data flow becomes apparent, and the usefulness of redux for keeping state within the unidirectional loop becomes also apparent.
After getting the key idea, the reader will have an easy time understanding tools built around it, ways to fold up the boilerplate, etc. A react/redux app can be very compact and approachable, even when the logic is complex.
Some apps are complex enough that they need this. In my experience, a lot of Redux apps really aren’t all that complicated, and people have used Redux because it’s The Thing To Do.
You start reaping the benefits when you need a state machine to manage complexity of your application. Thus, to demonstrate Redux's usefulness you need sufficiently complex examples.
That said, the explanation is as easy as saying Redux enables your frontend to be a state machine.
Reducers are very useful and simple to understand, and are great even on smaller apps. It's possible to see that in things like useReducer.
IMO the problem with Redux is that you "need" all the boilerplate around it. I say "need" in quotes because you can easily get around it and not lose much (if anything). And if you do, you start to reap the benefits even in very small apps.
https://redux-starter-kit.js.org/tutorials/advanced-tutorial
And yeah... a lot of the stuff normally associated with Redux is _intended_ as a "large project maintainability best practice" kind of thing.
The context is ngrx but the territory is familiar. Two things struck me in particular: one (the key point of the talk) is the idea that actions should be named as effects not intentions. The other (which I loved) is the idea of having action types that look like (say) "[Todo List Page] Add Todo Clicked" rather than "ADD_TODO" — it certainly makes the history more legible in redux-devtools. (I'm unsure how to name my consts then, though; I'm going with TODO_LIST_PAGE_ADD_TODO_CLICKED but it's very verbose; in the video it looks like he's not bothering with consts at all, and is just using the strings directly at dispatch and reduce time, and maybe there's something in that — the strings are specific enough that there'd be no scope for confusion, I guess.)
Just wondered what you thought of all this? I've read your idiomatic redux series, but maybe your thoughts have moved on since then...
Just going by your summary:
Yeah, I do find it interesting how some different parts of the community have chosen to format their action strings differently. Redux itself literally doesn't care, of course - the only requirement is that `action.type` not be undefined.
As I said in "The Tao of Redux, Part 1: Implementation and Intent" [0], one of the main intents behind Redux is that the action log should be a literally readable history of what's going on in your app with semantic meaning behind each name. You _could_ have a single `"SET_DATA"` action, but that doesn't tell me anything about what's going on.
The Redux docs originally went with the SCREAMING_SNAKE_CASE string format just because that's the common programming world convention for constant values. The naming in the docs also tends towards matching how you would name the action creator, ie, `"TOGGLE_TODO"` -> `toggleTodo()`. Those are perfectly legitimate choices to make, but definitely not the only possible ways to do it.
I've seen plenty of other variations over the years, usually in the form of some "domain" string + the "event" name. For Redux Starter Kit, we've gone with a '/'-separated string, such as `"todos/addTodo"`. The end user is still supplying both parts (via the slice `name` field and the user-defined reducer function name). So, the Angular-ish convention of `"[Some Domain] Some Event"` is perfectly fine, and very readable.
Either way, it doesn't matter what you actually name the _variable_ that points to that action type.
One of the most interesting variations I've seen on this is Slack, which apparently uses entire _sentences_ as their action types, each including a unique numeric ID [1].
Finally, yes, we would generally recommend treating actions semantically as "events in the app", not "setters at a distance". Again, the Redux store itself doesn't care, but the "events" mental modeling promotes dispatching fewer total actions and having multiple reducers independently responding to a given action [2]. We'll emphasize this more in the upcoming docs revamp.
Glad to know my writing has helped! Lemme know if you've got any other questions.
[0] https://blog.isquaredsoftware.com/2017/05/idiomatic-redux-ta...
[1] https://twitter.com/erikras/status/1153350835738468363
[2] https://redux.js.org/faq/actions#is-there-always-a-one-to-on...
All makes sense. I hadn't heard of RSK until I read this comment, but I've now seen your other comment in this thread which mentions it[0], watched the "State of Redux" talk referenced there and I'm excited to try it out. Cheers!
Well here's the issue, because if the project docs are unnecessarily complex then what hope does anyone have of learning the correct way? Come to HN and read your comment?
No–the official documentation needs to speak for itself and be judged on its own merits. If you feel so strongly about how Redux should be taught, you can always send them a PR rearranging the docs and stay on them until it gets merged :-)
See my proposed changes here:
https://github.com/reduxjs/redux/issues/3313#issuecomment-45...
The same way one learns about anything the correct way; the Tao that can be told is not the true Tao.
> No–the official documentation needs to speak for itself and be judged on its own merits.
Sure the documentation needs to stand on its own merits, as does he code itself. That's different than judging the code by the (de)merits of the documentation though.
“Redux is too complex” and “Redux has less-than-ideal documentation from an introductory perspective” are both potential problems, but they are distinct problems.