与Redux反应? “背景”问题怎么样? [英] React with Redux? What about the 'context' issue?

查看:100
本文介绍了与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'的Magic。

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



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

使用context通过组件传递模型数据。通过树明确地线程化数据更容易理解......

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'Context'功能,而不是通过'props'将 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.


  • 我是否正确地阅读了这一切...?

  • 目前的一般共识是什么?这个问题..?

推荐答案

上下文是一项高级功能,可能会有所变化。在某些情况下,它的便利性超过了它的缺点,所以像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.

最终反应Redux仍然支持将商店作为道具传递,所以如果你想完全避开上下文,你就有了这个选择。但是我会说这是不切实际的。

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天全站免登陆