3 ms·
>> state passed around as props Context provides a way to pass data through the component tree without having to pass props down manually at every level. https
by krishanath 8y ago
>> state passed around as props
Context provides a way to pass data through the component tree without having to pass props down manually at every level.
https://reactjs.org/docs/context.html https://reactjs.org/docs/context.html
>> single source of truth
Often, several components need to reflect the same changing data. We recommend lifting the shared state up to their closest common ancestor.
https://reactjs.org/docs/lifting-state-up.html https://reactjs.org/docs/lifting-state-up.html
- applecrazy 8y agoI guess then I have no idea why Redux is so widely used, given now the Context API exists.
- alexkoeh 8y agoRedux the library is not "state management for react". It's more of a concept that gives you a predictable structure around how state changes over time. I think what you're thinking of is specifically the `react-redux` library which gave a nice abstraction to the old & clunky React context API. The new context API was released with React 16.3 (which came out March of this year).
- acemarke 8y agoMultiple reasons. First, Redux has been around since 2015 (and won the "Flux Wars" that had been going on for a year before that), whereas the new `createContext` API only came out a few months ago. Second, Redux provides a lot more guidance around how you're supposed to structure your application. Context only provides a method for making values available to deeply nested components in the tree. Third, the Redux ecosystem is huge, with addons available for effectively any use case you can think of. [0] Please see my post "Redux - Not Dead Yet!" [1] and Dave Ceddia's post "Redux vs the React Context API" [2] for more details on how they relate. [0] https://blog.isquaredsoftware.com/2017/09/presentation-might-need-redux-ecosystem/ https://blog.isquaredsoftware.com/2017/09/presentation-might... [1] https://blog.isquaredsoftware.com/2018/03/redux-not-dead-yet/ https://blog.isquaredsoftware.com/2018/03/redux-not-dead-yet... [2] https://daveceddia.com/context-api-vs-redux/ https://daveceddia.com/context-api-vs-redux/
- beaconstudios 8y agothe new Context API replaces the `connect()`/`<Provider />` functionality, but not the flux data flow model. Nor does it provide a replacement for the surrounding redux ecosystem - so there's still plenty of benefit to using redux.
- tptacek 8y agoRedux is overused, because of a meme that suggests that the de facto standard React "stack" is React, Redux, and React-Router. That's just a meme (Dan Abramov even wrote a blog post debunking it!) but some memes are hard to fight.
- mikewhy 8y agoThe new Context API can't be used in lifecycle methods, which is a bit of a letdown.
- kabes 8y agoNote that Context is a pretty new API (1), way newer than most state management libraries. Before that it quickly became unmanageable to lift state up and pass props down all the way. [1] https://github.com/facebook/react/blob/master/CHANGELOG.md#1630-march-29-2018 https://github.com/facebook/react/blob/master/CHANGELOG.md#1...