How to set up React Context?

Follow these simple steps to set up react context in a new react project.

Step 1: Create a react project folder, open the terminal, and write the following command.

# Run this to use npm
npx create-react-app foldername
# Or run this to use yarn
yarn create react-app foldername

Step 2: Navigate to the root directory of your project using the following command.

cd foldername

Step 3: Creating a Context

We can create context by using the createContext() method given by React. See the below code snippet.

// Creating a context
import { createContext } from "react"
const AppContext = createContext();

The best practice is to create a separate folder named “context” and there you can create any number of contexts.

Step 4: Providing Context to Components

All those components that want to use data from the context must wrapped around the Context Provider.

// Providing the Context
const App = () => {
return (
<AppContext.Provider value={/* your shared value */}>
{/* Components that can access the context value */}
</AppContext.Provider>
);
}

Step 5: Using Context in Component

To use data from context, a component can use the “useContext” hook provided by React.

const Child = () => {
const contextValue = useContext(AppContext);
}

That’s how we can use Context in the React project.

What is the use of React Context in React-Redux?

React Context is a feature that React provides us to manage states required in multiple components. Redux is also a state management library and solves the same problem that React Context does but in a different way. In this article, we will see in detail what is react context, why and how to use it, how it is related to and different from react-redux, and then at last we’ll see which one to use and when.

Table of Content

  • What is React Context?
  • Why to use React Context?
  • How to set up React Context?
  • How React Context is related to React-Redux?
  • What to use and When?

Similar Reads

What is React Context?

React Context creates a new data layer apart from virtual DOM, to make states and data easily accessible to all the components. That means if any component wants to access some data, it can subscribe to context and use or update the data as per its requirement. Whenever any state in the context gets updated by any component, all the components that are subscribed to context re-render automatically....

Why to use React Context?

React Context solves two major problems that using state and props can’t be solved....

How to set up React Context?

Follow these simple steps to set up react context in a new react project....

How React Context is related to React-Redux?

Both react context and react-redux can be used for managing the state in the react app. They have similarities and difference both....

What to use and When?

Deciding which one to use depends on multiple factors like the size and complexity of the app. So, use React Context when you have simple state management and your app size is smaller and Redux comes in handy when you have complex state management and the app size is bigger....

Contact Us