你应该用 React Context Provider 包装你的整个应用程序吗? [英] Should you wrap your whole app with a React Context Provider?

查看:38
本文介绍了你应该用 React Context Provider 包装你的整个应用程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户已通过身份验证并可以通过 访问,我想保存一个简单的布尔值 + 用户 ID反应上下文 API.

许多指南用上下文提供者包装了他们的根组件.对我来说,包装整个应用程序似乎很浪费.另一方面,我需要在我的所有主页中都提供这些信息.

使用 React Context Provider 包装整个应用是否有任何负面影响?

示例:

class App 扩展组件 {使成为() {返回 (<我的提供者><div className="应用程序"><h1 className="App-title">欢迎来到我的网上商店</h1><产品列表/>

</MyProvider>);}

}

我使用了本指南作为参考.

我在 React Redux 方面没有任何经验,所以对于以前使用过这个框架的每个人来说,这可能很自然.

研究 Google 提供了许多关于如何实现 React Context 或使用 HOC 的指南,但我点击的 15 个没有回答我的问题.

解决方案

@Fyodor 在他的评论中指出:

<块引用>

您可以将整个应用程序包装在提供程序中.React-redux 推荐相同的(react-redux.js.org/introduction/...),所以你可以认为它可以接受练习

I want to save a simple boolean + userID if a user is authenticated and have it accessible via React Context API.

Many guides wrapped their root compoennt with the context Provider. To me it seems wastefull to wrap the whole app. On the other hand I need this information to be available in all my main pages.

Does it have any negative consequenses to wrap your whole app with a React Context Provider?

Example:

class App extends Component {
render() {
    return (
        <MyProvider>
            <div className="App">
                    <h1 className="App-title">Welcome to my web store</h1>
                <ProductList />
            </div>
        </MyProvider>
    );
}

}

I used this guide as a reference.

I do not have any experiences in React Redux so this might just natural to everyone who has used this framework before.

Researching Google brings up many guides on how to implement React Context or use HOC but the 15 I clicked did not answer my question.

解决方案

@Fyodor pointed it out in his comment:

You can wrap whole app in provider. React-redux recommends the same (react-redux.js.org/introduction/…), so you may consider it acceptable practice

这篇关于你应该用 React Context Provider 包装你的整个应用程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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