useContext is a React Hook that allows functional components to subscribe to a context.
React useContext hook provides a way to pass data through the component tree without having to pass props manually at every level.
useContext simplifies the process of consuming values from a React context within a functional component.
React useContext Hook allows us to access the value of a context without wrapping the component in a `Context.Consumer` unlike in Class Component.
For Managing State in React Application, `createContext` API is suitable for a small project or less complex architecture of an application, when it comes to large or complex projects it is preferred to use state management libraries such as "@reduxjs/toolkit".
// Yourcontext.js import { createContext } from 'react'; // create a Context instance with default value `null` const Yourcontext = createContext(null); export default Yourcontext;
// ParentComponent.js import React from 'react'; import Yourcontext from './Yourcontext'; const ParentComponent = () => { const contextValue = 'Hi, value from Context'; return ( <Yourcontext.Provider value={contextValue}> {/* Children components can access the context by subscribing to context */} <ChildComponent /> </Yourcontext.Provider> ); }; export default ParentComponent;
// ChildComponent.js import React, { useContext } from 'react'; import Yourcontext from './Yourcontext'; const ChildComponent = () => { const contextValue = useContext(Yourcontext); return ( <div> <p>Context Value: {contextValue}</p> </div> ); }; export default ChildComponent;
`Yourcontext` is created using `createContext()` in a separate file.
In ParentComponent, the `Yourcontext.Provider` is used to provide a value to the context. The value prop of the provider sets the context value that will be available to its descendants.
ChildComponent uses the `useContext hook` API to access the value provided by the context.
When ChildComponent renders inside the `Yourcontext.Provider`, it can access the context value without passing it through props.
If the context value changes, the ChildComponent will automatically re-render to reflect the updated value.
// Profilecontext.jsx import { createContext } from 'react'; // create a Context instance with default value `null` const Profilecontext = createContext(null); export default Profilecontext;
import ProfileContext from "./profileContext"; import ChildComponent from "./childComponent"; function ParentComponent() { return ( <ProfileContext.Provider value={{userProfile: {fullname: "Alice Collin", age: 24}}}> <ChildComponent /> </ProfileContext.Provider> ); } export default ParentComponent;
import { useContext } from "react"; import Profilecontext from "./profileContext"; function ChildComponent() { const contextData = useContext(Profilecontext); return ( <div> <h2>Full Name: {contextData?.userProfile?.fullname}</h2> <h3>Age: {contextData?.userProfile?.age}</h3> </div> ); } export default ChildComponent;
It enhances code reusability, improves code readability, and prevents prop drilling by allowing us to share values across the component tree efficiently.