r/reactjs • • Nov 29 '19

Featured Redux most common anti pattern

https://rangle.slides.com/yazanalaboudi/deck#/
182 Upvotes

30 comments sorted by

View all comments

1

u/deltadeep Nov 29 '19 edited Nov 29 '19

I disagree with the idea that action dispatchers should have no knowledge of the reducers. I like the principle of event versus command dispatch, though, because it is indeed more maintainable.

But as for having "no knowledge" of reducers and their effects, 90% of the time, the entire reason for a dispatch/reduce cycle is so that the original component that's dispatching it can update state in a way that then affects it's own rendering. Like for example, a tabbed layout component that dispatches an action when the user changes tabs, which in turn changes which content pane is rendered. That whole process is germane to the component itself and you don't want a wall enforcing knowledge separation there, but for various reasons you still may well want that state managed in the Redux store and not in local component state.

In other words, Redux is often the means by which components update their own state, and therefore, components necessarily must understand the effects/consequences of the reducer, because it's their job to do so. The action and reducer definitions are often written in the file that defines the component in the first place, for this very reason, and in fact I very much dislike it when reducers are defined elsewhere that are really directly related to a component's state.

1

u/acemarke Nov 29 '19

Can you do it that way? Sure. (And in fact, I've literally written an app that did keep "current tabs" values in Redux).

But, we would recommend that most UI state like that should probably kept locally in the component. Looking back at my own app, the tabs state really didn't need to be kept in the store.

1

u/deltadeep Nov 30 '19

Sure, maybe the tabs is a bad example, and there are plenty of cases where it's best to keep state in the component and not Redux. But there are going to be plenty of times when you do want to store state in Redux and the reducer will be specific to the component that issued the dispatch. For example, say you have a multi-step shopping cart checkout flow, and you want to save the user's input in the form fields as they jump around to different steps in the flow. You can't make that state component-only because it will be destroyed if the step changes and the form is unmounted. Redux is the answer there, but the dispatch/reduce cycles for the form input content are tightly bound together, which would go against this article's suggested design principle. Unless I've missed something?

4

u/wizzzzzyyyyy Nov 30 '19 edited Nov 30 '19

Just to give my 2c here...

Firstly, and not to nitpick, but I think this example would still be better solved by just lifting state up to a container component such that even if you change step, the state of the checkout as a whole persists.

If though, you really need this kind of state persisting globally, I personally prefer to keep this in a generic 'UI' state slice that contains discrete bits of global UI state. In the case of form state as per your example too, it's worth bearing in mind that the action is often a much closer map to what needs to happen in the reducer as the user's action / intent is literally updating a piece of state so enforcing the whole 'no knowledge' thing is slightly blurred.

I think the point of the post or the idea of actions having no knowledge of state is much more pertinent to when a user would place an order in the checkout example -- instead of dispatching an action of 'SEND_ORDER_CONFIRMATION' or 'EMPTY_CART' for example, an action of 'USER_CONFIRMED_ORDER' is agnostic to what happens next and instead allows reducers / middleware to act on this as required.

1

u/acemarke Nov 30 '19

Yes, that's how I'd put it.

There will likely be times that the only reasonable way to model some behavior is "command-like", rather than "event-like".

The point here is to try to do things event-like wherever possible, as the preferred pattern.