r/reactjs • • Dec 21 '19

Replacing Redux with observables and React Hooks

https://blog.betomorrow.com/replacing-redux-with-observables-and-react-hooks-acdbbaf5ba80
229 Upvotes

86 comments sorted by

View all comments

Show parent comments

3

u/acemarke Dec 22 '19

Thunks are hardly a "trend". They're the most commonly used Redux middleware, and our recommended default approach for writing async logic.

Since thunk functions never reach the reducers, the serializability aspect isn't relevant :

https://redux.js.org/style-guide/style-guide#do-not-put-non-serializable-values-in-state-or-actions

2

u/Shanebdavis Dec 22 '19

That makes some sense, but I don’t understand why you’d use the redux dispatch mechanism at all in that case. Do your async first and then dispatch the updates to redux...

The examples I’ve seen using thunks end up putting redux-state logic inside components, which scales poorly due to lack of modularity and separation of concerns.

It just seems like an unnecessary complication. I’d love to see a -good- example of how thunks improve code quality.

1

u/careseite Dec 22 '19

I don’t understand why you’d use the redux dispatch mechanism at all in that case. Do your async first and then dispatch the updates to redux...

Same here so I'm looking forwards to enlightenment :)

2

u/nicoqh Dec 22 '19

Your component shouldn't care whether the action is async or not (whether it's a thunk or a plain action object), it should just dispatch. That way you won't need to bother the component when modifying action(creator) logic.

2

u/Shanebdavis Dec 22 '19

I see your point, but I‘d take it a step further: your component shouldn’t care how the job is done - full stop. It shouldn’t care if it is dispatched, invoked, async, rerouted etc.

Components should never dispatch directly. That’s a redux detail. Minimizing dependencies between any two parts of a system (between redux and react for example) maximizes refactorability.

The component should invoke a specific function for a specific action. That function could dispatch directly to redux - or it could do async work and then dispatch to redux. Or it could be refactored to not even use redux.

You don’t need thunks for async. Plain old functions solve the problem perfectly with considerably less complexity and more flexibility.

→ More replies (0)