Hacker News new | ask | show | jobs
by reboog711 1682 days ago
> But the way react deals with state makes it so much easier to build and maintain complex applications.

Real question; not being snippy...

I didn't think React had any state management included. A lot of people I've spoken to seem to use Redux, a third party library for handling state.

Is my understanding incorrect?

3 comments

Hi, I'm a Redux maintainer. React has always had "state management" built in in the form of component state: `this.setState` in class components, and `useState/useReducer` in function components. Those work just fine.

However, that _is_ limited in that you can only access state inside the current component, or pass it down the tree as props/context. If you need to access data broadly across the component tree, or the data itself does not lend itself to a tree-shaped usage pattern, or you need to minimize the number of components that re-render when you update, or you need to use it _outside_ the component tree, then you may want to look at an external state management library.

I've talked about the tradeoffs in these approaches numerous times:

https://blog.isquaredsoftware.com/2021/01/context-redux-diff...

https://changelog.com/posts/when-and-when-not-to-reach-for-r...

https://blog.isquaredsoftware.com/2020/05/blogged-answers-a-...

All that said: React makes writing an application with any meaningful amount of complexity _vastly_ easier to do than trying to do it with either preceding UI frameworks (AngularJS 1.x, Backbone, etc) or jQuery. It isn't perfect, but it's well-suited for a wide range of application development use cases.

Reacts out of the box "useState" along side the unidirectional data flow acts as a lightweight, yet very powerful form of state management.

Redux takes that to the next level but is based on similar principles of "reactive" programming. For complex state heavy applications it's extremely powerful, but often overkill for many smaller applications.

React has 'useReducer' now, which can replace redux for most use cases.

But I also think 'react' means two different things. One the one hand, it's "just a library". In industry, it tends to mean... react + JSX + react-router + redux + react-this + react-that + react-redux-this-and-that all tied together with create-react-app. Plus its own command line tools, and browser plugins. It starts to feel very much like a framework then, if not a platform of its own.