与 Redux 反应?“上下文"问题呢? [英] React with Redux? What about the 'context' issue?

查看:20
本文介绍了与 Redux 反应?“上下文"问题呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常在 Stack 上发布与代码相关的内容,但这更多是关于社区的总体想法的问题.

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

似乎有很多人提倡使用 Redux 和 React 来管理数据/状态,但是在阅读和学习两者的过程中,我发现了一些不太正确的东西.

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.

在本页底部:http://redux.js.org/docs/basics/UsageWithReact.html(通过商店)它推荐使用 React 'Context' 的魔法".

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.

我们推荐的选项是使用一个特殊的 React Redux 组件,该组件被称为神奇地使存储可用于所有容器组件...

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

反应

在 React Context 页面 (https://facebook.github.io/react/docs/context.html) 它在顶部有一个警告:

React

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

上下文是一项高级和实验性功能.API 可能会在未来的版本中发生变化.

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

然后在底部:

正如在编写清晰的代码时最好避免使用全局变量一样,在大多数情况下您应该避免使用上下文...

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...

所以...

Redux 建议使用 React 的上下文"功能,而不是通过道具"将 store 向下传递到每个组件.而 React 建议相反.

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.

此外,似乎 Dan Abramov(Redux 的创建者)现在为 Facebook(React 的创建者)工作,只是为了让我更加困惑.

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

  • 我读得对吗..?
  • 目前对这个问题的普遍共识是什么......?

推荐答案

Context 是一项高级功能,可能会发生变化.在某些情况下,它的便利性超过了它的缺点,因此尽管具有实验性质,但 React Redux 和 React Router 等一些库还是选择依赖它.

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.

这里的重要部分是图书馆这个词.如果上下文改变其行为,我们作为图书馆作者将需要调整.但是,只要库不要求您直接使用上下文 API,您作为用户就不必担心对其进行更改.

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 在内部使用上下文,但它没有在公共 API 中公开这个事实.所以你应该觉得通过 React Redux 使用上下文比直接使用上下文要安全得多,因为如果它发生变化,更新代码的负担将落在 React Redux 而不是你身上.

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.

最终 React Redux 仍然支持始终将 store 作为 prop 传递,因此如果您想完全避免上下文,您可以选择.但是我会说这是不切实际的.

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:避免直接使用上下文,除非您真的知道自己在做什么.使用内部恰好依赖上下文的库相对安全.

这篇关于与 Redux 反应?“上下文"问题呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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