Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
105
rated 0 times [  112] [ 7]  / answers: 1 / hits: 16420  / 8 Years ago, tue, april 5, 2016, 12:00:00

I normally post code related stuff on Stack, but this is more a question about what the general thoughts of the community are.



There seems to be a lot of people advocating the use Redux with React to manage data/state, but while reading and learning both I've come across something that doesn't quite look right.



Redux



At the bottom of this page: http://redux.js.org/docs/basics/UsageWithReact.html (Passing the Store) it recommends using the Magic of React 'Context'.




One option would be to pass it as a prop to every container component. However it gets tedious, as you have to wire store even through presentational components just because they happen to render a container deep in the component tree.



The option we recommend is to use a special React Redux component called to magically make the store available to all container components...




React



On the React Context page (https://facebook.github.io/react/docs/context.html) it has a warning at the top:




Context is an advanced and experimental feature. The API is likely to change in future releases.




Then at the bottom:




Just as global variables are best avoided when writing clear code, you should avoid using context in most cases...



Do not use context to pass your model data through components. Threading your data through the tree explicitly is much easier to understand...




So...



Redux recommends using the React 'Context' feature rather than passing the store along down to each component via 'props'. While React recommends the opposite.



Also, it seems that Dan Abramov (the creator of Redux) now works for Facebook (the creator of React), just to confuse me more.




  • Am I reading all this right..?

  • What is the general current consensus on this issue..?


More From » reactjs

 Answers
70

Context is an advanced feature and is subject to change. In some cases its conveniences outweigh its downsides so some libraries like React Redux and React Router choose to rely on it despite the experimental nature.



The important part here is the word libraries. If context changes its behavior, we as library authors will need to adjust. However, as long as the library doesn’t ask you to directly use the context API, you as the user shouldn’t have to worry about changes to it.



React Redux uses context internally but it doesn’t expose this fact in the public API. So you should feel much safer using context via React Redux than directly because if it changes, the burden of updating the code will be on React Redux and not you.



Ultimately React Redux still supports always passing store as a prop so if you want to completely avoid context, you have that choice. However I would say this is impractical.



TLDR: Avoid using context directly unless you really know what you are doing. Using a library that happens to rely on context internally is relatively safe.


[#62685] Sunday, April 3, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
josefn

Total Points: 251
Total Questions: 93
Total Answers: 84

Location: Senegal
Member since Fri, Aug 21, 2020
4 Years ago
;