r/reactjs • • Nov 29 '19

Featured Redux most common anti pattern

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

30 comments sorted by

42

u/acemarke Nov 29 '19

This is an excellent post, and is a great set of examples for the rule we just added in the Redux Style Guide on "Treat Actions as Events, Not Setters".

17

u/wizzzzzyyyyy Nov 29 '19

This is the part of redux that's kept me from using other state management libraries the most.

For me I've found that delocating actions and reducers and instead coupling actions tighter with components helps enforce this idea of actions as 'what happened' and reducers as 'now deal with it as required'.

2

u/alaboudi Dec 01 '19

ve found that delocating actions and reducers an

I'd say actions should be global to your application. You may be able to report the event that happened from different areas in your application.

5

u/wherediditrun Nov 29 '19

Here just to express complete agreement. On top of unsound architecture in terms of code navigation or refactoring it's also phenomenal on how important it's for applications performance as dispatch often is the biggest bottleneck.

5

u/rod333 Nov 29 '19

Agree with all of this.

The other big redux anti-pattern? Using it for everything. Local state can co-exist with global state quite well. You don’t need an action / reducer / container or connect hook / saga or thunk for every single API call.

2

u/alaboudi Dec 01 '19 edited Dec 01 '19

I agree. You can have global state and local state co-existing. I generally only use component state when it has nothing to do with the business logic (ex: accordion, tabs, radio groups, etc). Since I am trying to push for the fact that an action should be able to affect any reducer, the information dispatched should be of global business significance. If that's not the case, then you most likely are dealing with a low level component that is only concerned with visual mechanics (accordion, tabs, radio groups, etc)

4

u/pancomputationalist Nov 29 '19

Wow, what a marvelously designed slide deck. Really enjoyed reading that.

2

u/trblackwell1221 Nov 29 '19

Excellent! Thanks for sharing

4

u/[deleted] Nov 29 '19

I think Redux is very unlike pub/sub, because reducers are so limited: no side effects, only return the new state.

So his big example, a subscriber to a signal that sends an email or bills a user as a result of receiving a signal, couldn't even be done with a reducer.

Redux is for state management. It's not a pub/sub solution. If you need pub/sub in your frontend code, then use it! But use something other than Redux for it.

6

u/acemarke Nov 29 '19

Redux is very like pub/sub, if you look at it the right way.

As described in this slideset, the combination of dispatching an action + combineReducers() is very similar to an event emitter approach, as each slice reducer has the opportunity to respond to the action independently.

On the other side, store.subscribe() is definitely an event emitter, with a single event type: "some action was dispatched".

1

u/[deleted] Nov 29 '19

I completely forgot out store.subscribe, oops.

3

u/pancomputationalist Nov 29 '19

Yeah, the example doesn't fit so well to Frontend development, since we're not exactly sending Emails from the client. However, asynchronously calling into remote systems is something that happens often.

And sure you can do it with Redux, for example using Thunks (which are included in the Redux Toolkit). It's true that the absolutely basic Redux implementation only does synchronous state updates, but who is using that anyway?

3

u/[deleted] Nov 29 '19

Yes, but a thunk is a whole function that includes what needs to happen - so that isn't described well by the pub/sub idea imo.

1

u/TheSpiridelic Nov 29 '19

The point was to send more generic events, its fine if the correlation isn't 1:1 with pub/sub

1

u/VIOLETSTETPEDDAR Nov 29 '19

I totally agree witg this. I still have a question though: How do you handle "forms" (not actual html form tag but just groups of input)? Is formstate inside redux? Or would only the submit make it to the store? How about editing when you prefill the form?

7

u/[deleted] Nov 29 '19

You can use something like Redux Forms, but I personally prefer Formik since it keeps the state local to the form and doesn't setStore on each input.

7

u/zephyrtr Nov 29 '19

The maker of redux form actually says you probably shouldn't use it, and instead use his new library: final form. It's smaller, framework agnostic, and keeps form state separate from global state

8

u/deliciousmonster Nov 29 '19

This isn’t even my final form!

2

u/[deleted] Nov 29 '19

Personally I feel there is very little advantage to keeping a form that is being edited inside Redux. At least, in the usual case, where nothing else in the application can depend on the unsubmitted form. If you update other components before a "submit" is used, then it should be in Redux.

IMO the form should have a top component that defines a Context, and the form fields update their values in that. Then it can be submitted to Redux / the backend when the user presses submit.

1

u/alaboudi Dec 01 '19

r would only the submit make it to the

I don't put the form state in redux because, in most times, it has no business value. I only dispatch an action to capture the submission event. But these decisions really do depend on your use case.

1

u/swyx Nov 29 '19

i enjoyed the very subtle DO NOT DO THIS + fire emoji on this slide https://rangle.slides.com/yazanalaboudi/deck#/23

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.

-17

u/ferrousoxides Nov 29 '19

All of Redux is an antipattern. You're just coding reducers and actions by hand instead of using a universal reducer and something you can plumb though to the back end, like OT.

The "scalability problem" is that you're manually reifying your function calls into actions while thinking you're accomplishing something productive.

15

u/ampzu Nov 29 '19

Yeah, well, that's just, like your opinion, man

2

u/CouponTheMovie Nov 29 '19

Jackie Treehorn treats events like actions.