Table of contents
No headings in the article.
Today we will be discussing the use of the useContext hook in React, which allows for the efficient sharing of data across components and simplifies component communication.
The useContext
hook in React allows for efficient sharing of data across components without the need for props drilling.
It simplifies component communication by providing a way for components to access global state without having to pass props down through multiple levels of components.
This results in a more organized and maintainable codebase, as well as improved performance by avoiding unnecessary re-renders. Additionally, useContext can be used as an alternative to other state management libraries such as Redux.
Here is an example of how to use the useContext
hook in a React component:
import React, { useContext } from 'react';
// Create the context
const MyContext = React.createContext();
// Provide a context value
function MyProvider({ children }) {
const [value, setValue] = React.useState("Hello, World!");
const providerValue = { value, setValue };
return <MyContext.Provider value={providerValue}>{children}</MyContext.Provider>;
}
function MyComponent() {
// Use the context
const { value, setValue } = useContext(MyContext);
return (
<div>
<p>{value}</p>
<button onClick={() => setValue('Hello, React!')}>Change Value</button>
</div>
);
}
function App() {
return (
<MyProvider>
<MyComponent />
</MyProvider>
);
}
In this example, we first create a context object by calling React.createContext()
. This creates an object that we can use to create a provider component and a consumer component.
The MyProvider
component is a component that provides a value to the context. It uses the useState
hook to set an initial value for the context. The MyProvider
component then renders a MyContext.Provider
element, which wraps its children and provides the value to the context. The value
and setValue
are passed as a single object to the provider.
The MyComponent
component is a component that consumes the value from the context using the useContext
hook. The useContext
hook is called with the context object that we created earlier, and it returns the current value of the context. In this example, the value of the context is a string, but it could be any type of data.
The MyComponent
component then renders a div element with a paragraph element that displays the value from the context and a button element, when the button is clicked, it calls the setValue function, which will change the value of the context and re-render the component to display the updated value.
Finally, the App
component is the top-level component that renders the MyProvider
component and the MyComponent
component. This component is responsible for providing the context value to the MyProvider
component and rendering the MyComponent
component.
When the App component is rendered, it renders the MyProvider component, which sets the value for the context, and MyComponent component, which retrieves the value from the context using the useContext
hook.
With this example, you can see how the useContext
hook allows you to easily share values between components without having to pass the values down through multiple levels of the component tree.
Hope you like it 🤗
Happy coding!
want to give suggestions:-
Email me at nidhisharma639593@gmail.com