Hacker News new | ask | show | jobs
by adatavizguy 2374 days ago
As someone who has web apps in production written in backbone.js, Angular, and React, I can say selectors with Reselect to transform all the data being passed into props, Sagas to manage all async workflows, and Ramda with Redux reducers is pure fire. There is no business logic in components or containers unless it is tied directly to the view and layout, not for the data. It is such an easy way to reason about huge amounts of data coming into the system from lots of different places. For performance, everything gets memoized based on object references. Using the immutable data structures in the store, Reselect keeps the transformed data cached with memoization until the object reference is changed in the store.
6 comments

Only problem with Redux is it can be misused quite easily. The codebase at work has performance issues because of Redux, rerender issues because of reselect (not confirmed yet) and imo very very hard to read code because of Redux-Thunk. The state management was implemented by one person and everyone else is terrified of touching that part of the codebase.

Im not blaming redux for this, this is clearly a problem with how we implemented redux. But our team of 3 devs would be much much better off by having used Mobx.

I've seen better implementations of Redux and https://github.com/isubasinghe/advanced-redux-patterns is one of them (this is code by Nir Kaufman for his advanced redux patterns talk)

Hi, I'm a Redux maintainer. If you've got any specific concerns I can help with, I'd be happy to try to offer advice.

Out of curiosity, what issues have you been seeing with using thunks?

I'd specifically encourage you to check out our new official Redux Toolkit package. It includes utilities to simplify several common Redux use cases, including store setup, defining reducers, immutable update logic, and even creating entire "slices" of state at once:

https://redux-toolkit.js.org

You might also want to look at the new "Style Guide" page we've added to the docs, which lists our recommended patterns and practices for using Redux:

https://redux.js.org/style-guide/style-guide

Hey thanks for reaching out, that's very nice of you :), I have no specific concerns atm but will reach out I do need help.

Issues with thunks are maybe(?) due to our implementation again, but we have way too many dispatches being called (100s of lines of dispatch calls for one action sometimes). It becomes quite hard to trace what happens when a certain business logic is executed because of this.

For example, say we have an action to delete a domain level entity, lets say this is a book, well books can have paragraphs, paragraphs can have sentences and sentences can have words (normalized state, terrible example I know, but all I could think of atm). Well the action to delete a book, has dispatches to delete all children entities as well. These dispatches actually may have other dispatches as well for ui stuff etc. Doing redux this way really didn't scale well for us, but I suppose we have very very unique, complex business logic compared to other projects.

The style guide looks excellent, I think this would have made the codebase easier to follow. I will defs be following this for my personal projects.

Thanks

Yeah, it sounds like the issue here is that you are trying to dispatch a single action per tiny state update. This is why we now recommend "modeling actions as 'events' instead of 'setters'". See the links I posted in this sibling comment for suggestions on how to change your conceptual approach:

https://news.ycombinator.com/item?id=21853160

Also, note that our new Redux Toolkit package will at least help simplify your existing logic, and you can begin migrating to it incrementally:

https://redux-toolkit.js.org

Thunks can be an anti-pattern. They encourage multiple dispatches to redux which can leave people designing "set data" style reducers rather than more meaningful ones. Parent comment suggests sagas and that's much better. An action in a component dispatches one thing and the saga can co-ordinate all of the logic.
> "set data" style reducers

I've heard this before but don't really understand it. Most of my reducers are storing and maybe updating data loaded from the server. What's wrong with this pattern?

Our new "Style Guide" docs page gives a recommendation to "model actions as 'events', not 'setters' [0], and there were two recent talks on this topic that go into a lot more detail [1] [2].

[0] https://redux.js.org/style-guide/style-guide/#model-actions-...

[1] https://github.com/dmmulroy/talks/blob/master/event-driven-r...

[2] https://youtu.be/K6OlKeQRCzo?t=2626 / https://rangle.slides.com/yazanalaboudi/deck#/

> "model actions as 'events', not 'setters'

I don't think actions are what they're talking about, but reducers as setters vs reducers with more complex logic. I stumbled against the same thing last week, and rather than duplicating the logic in two reducers I settled on putting the logic in the action and turning both reducers into simple setters.

I'm still new to redux and I'm learning using angular ngrx. I realize ngrx is just inspired by redux. What's the team relationship? Are these the same maintainers, is there a ngrx toolkit?
No, the Redux and NgRX teams are completely separate. We've occasionally chatted briefly online, but that's it.
I’m mostly with you there but I’ve found saga to be a double-edged sword. The library offers some KILLER features and when you’ve got the right use case, it’s perfect, but it’s so easy to abuse. Because it does so much, I found myself putting more and more responsibilities on it and wound up with some very magical feeling, hard to troubleshoot code.

What changed everything for me was adding GraphQL and getting the majority of my API communication and data storage out of redux. Now, sagas and redux are further down the list of tools I reach for, complexity is way down across the board, and the sagas and reducers that’s remain are more narrowly scoped and easier to reason about and maintain.

I'm a Redux maintainer. Sagas are a great power tool, but most apps don't need them [0]. We recommend that most apps stick to thunks as the default [1], and our new official Redux Toolkit package [2] adds thunks automatically to your store setup.

I've used sagas in a couple apps that truly did have very complex async workflows, and they were great for that use case. But yeah, using sagas _just_ for something like data fetching is definitely overkill.

[0] https://redux.js.org/faq/actions#what-async-middleware-shoul...

[1] https://redux.js.org/style-guide/style-guide/#use-thunks-for...

[2] https://redux-toolkit.js.org

As someone that had inherited a redux-thunks project, for me thunks are like calling fetch() directly inside your components callbacks, with extra steps. Really bad for testing and isolating code.

I haven't tried saga, just rxjs with redux which was definitely good. I've also worked with Elm, which supposedly inspired redux. Elm does it perfectly, I don't know why Redux missed async.

> I'm a Redux maintainer. Sagas are a great power tool, but most apps don't need them

I’m super happy that you’re here saying that. I’d go farther than “most apps” to “almost no apps” but, though I’ve mostly worked with apps where I wish they hadn’t used sagas, I have a lot of admiration for the saga concept and library.

> I found myself putting more and more responsibilities on it and wound up with some very magical feeling, hard to troubleshoot code

Agreed. There's a simplicity to React / Redux that I absolutely adore. There's very little magic going on: things happen because you've explicitly told them to. Side-effects in Redux seem to throw this out the window.

reselect has been magnificent to us (along with moving most fetching logic to thunks)

API shapes change and evolve, and mapping raw API shapes to components (through Redux) became a major PITA, real fast.

reselect solved all of that; we transform almost all data to a “view layer friendly” format before passing it into the components, keeping them as dumb as possible. We also moved most “view business logic” to the selector level: The check might involve user permissions, subscription plan, etc, etc, but the component only receives ‘shouldThisThingBeDisplayed‘. API shape/changes very rarely impact the components.

This approach makes it much easier to test in our experience. Business logic is tested without any concern for the components themselves. Visuals are covered by Storybook and ChromaticQA.

I could never get into the whole "side-effects for async code" pattern so many people are using with Redux. At my current gig, we are using redux-observable, and that's a WHOLE lot for people to grok to simply ... `await fetch('/users')`. I do agree, redux-sagas is much more in line with what I would expect.

But they all lead to things I would consider "hacky". Since it's all side effects, making sure two things that are "side effects" of the one action run in consistent order can be troublesome, since it's usually down to "well which one registered with the middleware first?"

And, if you ever need to actually simply `await` an action, you need to set up some middleware that will give you a promise that resolves / rejects when certain actions happen. Then you need to tie metadata to your actions if it stands that multiple of the action you're waiting to resolve happen.

Or, some people recommend creating a promise before dispatching, passing that promise reference around, checking for it in your sagas / observable.

All things I would consider super hacky.

I've been using my own middleware for years that simply lets you make `payload` a function, async function, or promise, and it's all incredibly more stable when things get complicated, and newcomers can grok whats going on in a second. You get `/start`, `/success`, `/error` actions baked in. And since it's promise-based, you can `await dispatch()` when you need to. If you need to get the state, or dispatch more actions, you can, right from the original action.

For times when you truly do want something to be a side effect (read: incredibly rarely because my actions have access to `dispatch` and `getState`), I use a very simple middleware like redux-saga but with standard async / await syntax.

I've been intrigued by people praising these side-effect workflows for some time, but after using redux-observable, I don't think I'll ever go near them again.

Apologies for the rant.

I've had good experiences using a simple combination of lit-html and the sam pattern. I'm not an evangelist yet, I want to try and throw some more complex edge cases at it first but so far, I've had no complaints.

1. https://lit-html.polymer-project.org

2. https://sam.js.org

Shout out to Rematch, it has been the go-to library to ail my redux woes. So much is simplified that it makes things way easier to grok.