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

1

7

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.

Demodena answered 1/7, 2019 at 1:4 Comment(3)
You can wrap whole app in provider. React-redux recommends the same (react-redux.js.org/introduction/…), so you may consider it acceptable practiceManymanya
If you're planning to use context throughout your application, then yes.Myongmyopia
Write is as an answer so I can accept it.Demodena
D
5

@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

Demodena answered 16/7, 2019 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.