DEV Community

Discussion on: Redux is half of a pattern (2/2)

Collapse
 
oliverradini profile image
OliverRadini • Edited

The issue for me has always been that if you make some subset of state local to a component, then that component also has to handle all logic relating to that slice of state.

Take a concrete example - a button which makes an api call. We want to display the loading state with some change of appearance in the button. If we hold the loading state local to the button component - then what's making the api call? How does the button component become notified of changes to the loading state? It either needs to be complexly wired up to some external code - in which case, why not just hold the state externally? Or else it has to have all the api-call code within the button component itself. The latter option seriously reduces the reusability of the component.

Strictly speaking, isolating state must mean that all logic surrounding that state may only be performed within the isolated component. I don't mean to say that this is necessarily a bad thing - I just haven't found many examples where state is truly isolated in this way.

I've found the best option to be injecting state as props (or, if we prefer, arguments of a function). Then we can manage state however we want, and we aren't letting the component make any decisions at all about where state should be managed. In my opinion, front end components have no business managing state - state is a far more complex than we ought to want to have in a UI component - let it be managed elsewhere, by whatever means is deemed appropriate for that use case. I don't mean to say that this is always, or ever, a global store, or Redux.

I really enjoyed this article and certainly found it thought-provoking; I had an issue with one part though. You wrote:

"Whenever something is done for the sole purpose of making something easy, it almost always makes some other use-case more difficult. Redux and its single-source-of-truth is no exception"

I agree that making things easy often leads to making other things more difficult. But I don't agree that Redux makes things easy. One of the chief arguments against Redux is that it makes all parts of interacting with state far more difficult; granted, it makes it very easily to blindly inject state once we've already got the boilerplate, but it makes it a great deal more difficult to add state in the first place - this to me is a big pro of using Redux.