React Context vs React Redux,我应该何时使用每一个? [英] React Context vs React Redux, when should I use each one?

查看:366
本文介绍了React Context vs React Redux,我应该何时使用每一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

React 16.3.0已发布并且上下文 API不再是实验性功能。 Dan Abramov(Redux的创建者)写了一篇很好的评论,这里关于这一点,但是当Context仍然是一个实验性功能已经2年了。



我的问题是,在你看来/经验我何时应该使用 React Context 超过 React Redux ,反之亦然?

解决方案

上下文不再是一个实验性功能,您可以直接在应用程序中使用Context,并且非常适合将数据传递给深度嵌套的组件,而这些组件就是为它设计的。



正如Mark erikson写的那样博客


如果你只是使用Redux来避免传递道具,那么上下文可能是
替换Redux - 但是你可能在第一个
的地方不需要Redux。



上下文也没有给你任何类似的东西code> Redux DevTools ,
跟踪状态更新的能力,中间件添加集中式
应用程序逻辑,以及 Redux
启用的其他强大功能。


Redux 功能更强大,并提供 Context Api 不提供的大量功能,也作为As @danAbramov 提到


React Redux在内部使用上下文,但它不会在
中公开这个事实公共API。所以你应该通过React
Redux使用上下文比直接感觉更安全,因为如果它改变了,更新
代码的负担将在React Redux而不是你。


它可以实现Redux实际更新其实现以符合最新的上下文API



最新的Context API可以是用于您只需使用Redux在组件之间传递数据的应用程序,但是使用集中数据并使用 redux-thunk 或<$ c在Action创建者中处理API请求的应用程序$ c> redux-saga 仍然需要redux。除此之外,redux还有其他库,例如 redux-persist ,它允许您将商店数据保存在localStorage中,并在刷新时重新水化,这是上下文API仍然不支持的。 / p>

正如@dan_abramov在他的博客中提到的您可能不需要Redux ,redux具有有用的应用程序,如



  • 将状态持久保存到本地存储,然后从中启动,开箱即用。

  • 在服务器上预填充状态,以HTML格式发送到客户端,然后从中启动开箱即用。

  • 序列化用户操作并将其与状态快照一起附加到自动错误报告中,以便产品开发人员
    可以重播它们重现错误。

  • 通过网络传递操作对象以实现协作环境而不会发生重大变化如何编写代码。

  • 维护撤消历史记录或实现乐观突变,而不会对代码的编写方式进行重大更改。

  • 在开发中的状态历史,并在代码更改时从操作历史中重新评估当前状态,
    la TDD。

  • 为开发工具提供全面的检查和控制功能这样产品开发人员就可以为他们的
    应用程序构建自定义工具。

  • 在重用大部分业务逻辑的同时提供备用UI。


使用这些应用程序很快就会说Redux将被新的Context API取代


React 16.3.0 was released and the Context API is not an experimental feature anymore. Dan Abramov (the creator of Redux) wrote a good comment here about this, but it was 2 years when Context was still an Experimental feature.

My question is, in your opinion/experience when should I use React Context over React Redux and vice versa?

解决方案

As Context is no longer an experimental feature and you can use Context in your application directly and is going to be great for passing down data to deeply nested components which what it was designed for.

As Mark erikson has written in his blog:

If you're only using Redux to avoid passing down props, context could replace Redux - but then you probably didn't need Redux in the first place.

Context also doesn't give you anything like the Redux DevTools, the ability to trace your state updates, middleware to add centralized application logic, and other powerful capabilities that Redux enables.

Redux is much more powerful and provides a large number of features that the Context Api doesn't provide, also as As @danAbramov mentioned

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.

Its upto Redux to actually update its implementation to adhere with the latest context API

The latest Context API can be used for Applications where you would simply be using Redux to pass data between component, however application which use centralised data and handle API request in Action creators using redux-thunk or redux-saga still would need redux. Apart from this redux has other libraries associated like redux-persist which allow you to save store data in localStorage and rehydrate on refresh which is what context API still doesn't support.

As @dan_abramov mentioned in his blog You might not need Redux, that redux has useful application like

  • Persist state to a local storage and then boot up from it, out of the box.
  • Pre-fill state on the server, send it to the client in HTML, and boot up from it, out of the box.
  • Serialize user actions and attach them, together with a state snapshot, to automated bug reports, so that the product developers
    can replay them to reproduce the errors.
  • Pass action objects over the network to implement collaborative environments without dramatic changes to how the code is written.
  • Maintain an undo history or implement optimistic mutations without dramatic changes to how the code is written.
  • Travel between the state history in development, and re-evaluate the current state from the action history when the code changes, a la TDD.
  • Provide full inspection and control capabilities to the development tooling so that product developers can build custom tools for their
    apps.
  • Provide alternative UIs while reusing most of the business logic.

With these many application its far too soon to say that Redux will be replaced by the new Context API

这篇关于React Context vs React Redux,我应该何时使用每一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆