Should you wrap your whole app with a React Context Provider?

谁都会走 提交于 2019-12-13 20:19:58

问题


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.


回答1:


@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



来源:https://stackoverflow.com/questions/56829091/should-you-wrap-your-whole-app-with-a-react-context-provider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!