与 Redux 的反应? “上下文”的问题怎么样?

我通常在 Stack 上发布与代码相关的内容,但这更多的是关于社区的一般想法是什么的问题。

似乎有很多人提倡使用 Redux with React 来管理数据/状态,但是在阅读和学习这两者的过程中,我遇到了一些看起来不太对劲的东西。

复制

在这个页面的底部: http://redux.js.org/docs/basics/UsageWithReact.html(通过商店) ,它建议使用“魔术”的反应“上下文”。

一种选择是将其作为一个道具传递给每个容器组件。然而,它变得单调乏味,因为即使是通过表示组件连接存储,也只是因为它们恰好在组件树的深处呈现一个容器。

我们推荐的选项是使用一个特殊的 React Redux 组件,该组件可以神奇地使存储对所有容器组件都可用..。

做出反应

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

上下文是一个高级的、试验性的特性。

然后在底部:

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

不要使用上下文通过组件传递模型数据。显式地通过树线程处理数据更容易理解..。

那么..。

Redux 建议使用 React‘ Context’特性,而不是通过‘ props’将 store传递给每个组件。而 React 的建议正好相反。

此外,似乎丹 · 阿布拉莫夫(Redux 的创始人)现在为 Facebook (React 的创始人)工作,只是为了让我更困惑。

  • 我没看错吧?
  • 目前在这个问题上的普遍共识是什么... ?
16071 次浏览

I don't know about others, but I prefer using react-redux's connect decorator to wrap my components so that only the props from the store I need are passed into my component. This justifies the use of context in a sense because I am not consuming it (and I know, as a rule, any code that I am in charge of will not consume it).

When I test my components, I test the non-wrapped component. Because react-redux only passed the props I needed on that component, I now know exactly what props I need when I'm writing the tests.

I suppose the point is, I don't ever see the word context in my code, I don't consume it, so to a certain degree, it doesn't affect me! This doesn't say anything about Facebook's "experimental" warning.. If context disappeared, I'd be just as screwed as everyone else until Redux was updated.

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.

There's an npm module that makes it really easy to add redux to the react context

https://github.com/jamrizzi/redux-context-provider

https://www.npmjs.com/package/redux-context-provider

import React, { Component } from 'react';
import ReduxContextProvider from 'redux-context-provider';
import createStore from './createStore';
import actions from './actions';
import Routes from './routes';


export default class App extends Component {
render() {
return (
<ReduxContextProvider store={store} actions={actions}>
<Routes />
</ReduxContextProvider>
);
}
}

React ships with all the features you need to handle your state without a single additional library. Most of your application's states should not be global as they live just fine in a useState or useReducer or custom hook next to your components.

So before you dive into the world of advanced state management (e.g. Redux), consider using the tools React ships with out of the box.

If you are interested in learning a bit more about this, I'd recommend this article by Andy Fernandez, which dives into the details on Redux: Context API vs Redux