Context API in React JS
Published by
sanya sanya
The Context API
in React is a feature that allows us to manage and share state between components without needing to pass props through every level of the component tree. It provides a way to create a global state that can be accessed by any component within its scope.
The Context API containes three main parts:
Context Object: This is the core part of the Context API
. It is created using the React.createContext() method. The context object is used to create a provider and a consumer for sharing the state.
Provider: The provider is a React component that wraps the part of the component tree where we can share the state. It accepts a value prop, which represents the data or state we want to share.
Consumer: The consumer is a React component that allows us to consume the shared state. It receives the value passed by the provider and can be used within its render function or as a hook using the useContext hook.
The example of how to use the Context API
is mentioned below.
-
1- Create a new context.
import React from 'react'; const MyContext = React.createContext(); export default MyContext;
-
2- Wrap the component tree with the provider.
// App.js
import React from 'react';
import MyContext from './MyContext';
import ComponentA from './ComponentA';
function App() {
const sharedState = "Hello from Context";
return (
<MyContext.Provider value={sharedState}>
<ComponentA />
</MyContext.Provider>
);
}
export default App;
- 3- Consume the shared state in a component.
// ComponentA.js
import React, { useContext } from 'react';
import MyContext from './MyContext';
import ComponentB from './ComponentB';
function ComponentA() {
const sharedState = useContext(MyContext);
return (
<div>
<h1>{sharedState}</h1>
<ComponentB />
</div>
);
}
export default ComponentA;

// ComponentB.js
import React, { useContext } from 'react';
import MyContext from './MyContext';
function ComponentB() {
const sharedState = useContext(MyContext);
return (
<div>
<p>{sharedState}</p>
</div>
);
}
export default ComponentB;
In this example, we create a simple context called `MyContext` in MyContext.js, wrap the ComponentA component with the provider in App.js, and then consume the shared state in both ComponentA and ComponentB using the useContext hook.
With the Context API, We can pass down data, state, or even functions to any level of the component tree without the need to explicitly pass props through each intermediate component. It's particularly useful for managing global state, such as user authentication, theme, or language preferences.
Library
WEB DEVELOPMENT
Basic
Frontend
Express JS
React
Hello World in ReactJS
Rendering Elements
States and Lifecycles in React JS
Handling Events in React JS
Introduction to JSX
Components and Props in React JS
Conditional Rendering
Lists and Keys in React JS
Introduction to Redux
Types of components (class vs functional)
Using the useEffect hook
Building custom hooks in React JS
Context API in React JS
Forms in React JS
Lifting State Up in React JS
Composition vs Inheritance
Prop drilling in React JS
Introduction to Hooks
Using the useState hook
UseMemo vs useCallback
Backend
Interview Questions
FAANG QUESTIONS