简体   繁体   English

我们也可以对容器使用React Context吗?

[英]Can we use React Context for containers as well?

This is React's explanation for use of Context 这是React对使用Context的解释

Context is designed to share data that can be considered “global” for a tree of React components, such as the current authenticated user, theme, or preferred language. 上下文旨在共享可被视为React组件树(例如当前经过身份验证的用户,主题或首选语言)的“全局”数据。

While this holds good for React components, is it practical to consider using context to share data between React containers as well? 尽管这对React组件很有利,但考虑使用上下文在React容器之间共享数据是否可行? Is there an alternative for Redux or MobX, inbuilt in React? React中内置Redux或MobX的替代品吗?

React Context can be used in place of something like Redux or Flux but you might want to look into using React Context for low-frequency updates (eg theme changes, user authentication) but not use it for the general state management of your application. 可以使用React Context代替Redux或Flux之类的东西,但是您可能希望研究使用React Context进行低频更新(例如,主题更改,用户身份验证),而不是将其用于应用程序的一般状态管理。

This is what Sebastian has to say regarding this....who is greatly involved with React development. 这就是塞巴斯蒂安对此所说的。。。

My personal summary is that new context is ready to be used for low frequency unlikely updates (like locale/theme). 我的个人总结是,新上下文已准备好用于低频不太可能的更新(例如语言环境/主题)。 It's also good to use it in the same way as old context was used. 以与使用旧上下文相同的方式使用它也很好。 Ie for static values and then propagate updates through subscriptions. 即获取静态值,然后通过订阅传播更新。 It's not ready to be used as a replacement for all Flux-like state propagation. 还没有准备好替代所有类似Flux的状态传播。 --- Sebastian Markbage --- 塞巴斯蒂安·马克巴奇

https://github.com/reduxjs/react-redux/issues/1177 https://github.com/reduxjs/react-redux/issues/1177

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM