Building Scalable React Apps with Context API: Unlocking the Potential of Data Sharing

Dev Balaji
3 min readMay 30, 2023

--

The Context API in React is a feature that allows you to share data between components without explicitly passing it through props at every level. It provides a way to pass data down the component tree without going through intermediary components that do not need the data. It is useful in scenarios where multiple components need access to the same data, such as user authentication status, theme settings, or language preferences.

To implement the Context API in React, you need to follow these steps:

  1. Create a Context:
    Start by creating a new Context object using the `createContext` function from the `react` package. This function returns two components: a Provider and a Consumer.
// ExampleContext.js
import React from 'react';
const ExampleContext = React.createContext();
export default ExampleContext;

2. Create a Provider Component:
The Provider component is responsible for supplying the data that needs to be shared with other components. It wraps the components that need access to the data.

// ExampleProvider.js
import React from 'react';
import ExampleContext from './ExampleContext';
const ExampleProvider = ({ children }) => {
const sharedData = 'This is the shared data';
return (
<ExampleContext.Provider value={sharedData}>
{children}
</ExampleContext.Provider>
);
};
export default ExampleProvider;

In this example, we’re using a simple string (`’This is the shared data’`) as the value, but you can use any JavaScript value or object.

3. Use the Context in Components:
Now, you can access the shared data in any component that is wrapped by the Provider. To access the data, you need to use the Consumer component or the `useContext` hook.

Using the Consumer component:

// ExampleConsumer.js
import React from 'react';
import ExampleContext from './ExampleContext';
const ExampleConsumer = () => {
return (
<ExampleContext.Consumer>
{value => <div>{value}</div>}
</ExampleContext.Consumer>
);
};
export default ExampleConsumer;

Using the useContext hook:

// ExampleHook.js
import React, { useContext } from 'react';
import ExampleContext from './ExampleContext';
const ExampleHook = () => {
const value = useContext(ExampleContext);
return <div>{value}</div>;
};
export default ExampleHook;

Both the Consumer component and the `useContext` hook allow you to access the shared data from the context. In this example, we’re simply rendering the value in a `<div>`, but you can use it in any way that suits your application.

4. Wrap Components with the Provider:
Finally, you need to wrap the components that need access to the shared data with the Provider component.

// App.js
import React from 'react';
import ExampleProvider from './ExampleProvider';
import ExampleConsumer from './ExampleConsumer';
import ExampleHook from './ExampleHook';
const App = () => {
return (
<ExampleProvider>
<ExampleConsumer />
<ExampleHook />
</ExampleProvider>
);
};
export default App;

By wrapping the components with the Provider component, they will have access to the shared data from the Context.

That’s the basic process of implementing the Context API in React. The Context API is flexible and allows you to create multiple contexts and nest them as needed to share different data with different parts of your application.

--

--

Dev Balaji
Dev Balaji

Written by Dev Balaji

🚀 Tech Enthusiast | 🌟 Mastering JavaScript & Frameworks | 💡 Sharing Tips & Tricks | 📘 Aspiring Blogger & Architect | 🐍 Python Practitioner

No responses yet