React context allows us to pass down and use (consume) data in whatever component we need in our React app without using props.
React context is great when you are passing data that can be used in any component in your application.
These types of data include:
Data should be placed on React context that does not need to be updated often.
Avoid props drilling: when you pass props down multiple levels to a nested component, through components that don’t need it.
There are four steps to using React context:
A way to consume context with the useContext
hook.
Instead of using render props
, we can pass the entire context object to React.useContext()
to consume context at the top of our component.
import React from 'react';
export const UserContext = React.createContext();
export default function App() {
return (
<UserContext.Provider value="Reed">
<User />
</UserContext.Provider>
)
}
function User() {
const value = React.useContext(UserContext);
return <h1>{value}</h1>;
}
React Context is a feature in React that allows you to share data throughout your application without having to pass props down through multiple levels of components. It provides a way to pass data through the component tree without having to pass props down manually at every level.
Context is created by using the React.createContext()
method, which returns an object with a Provider and Consumer component. The Provider component is used to provide the data to the components within its scope, while the Consumer component is used to access the data.
const MyContext = React.createContext();
function App() {
const data = {
message: "Hello World!"
};
return (
<MyContext.Provider value={data}>
<MyComponent />
</MyContext.Provider>
);
}
function MyComponent() {
return (
<MyContext.Consumer>
{data => <div>{data.message}</div>}
</MyContext.Consumer>
);
}
In this example, the data object is passed to the MyContext.Provider component as the value prop. The MyComponent component can then access this data by using the MyContext.Consumer
component.
Using context in React involves the following steps:
const MyContext = React.createContext();
function App() {
const data = {
message: "Hello World!"
};
return (
<MyContext.Provider value={data}>
<MyComponent />
</MyContext.Provider>
);
}
function MyComponent() {
return (
<MyContext.Consumer>
{data => <div>{data.message}</div>}
</MyContext.Consumer>
);
}
NOTE: The Provider component must be a parent component of the Consumer component for the Consumer component to access the data. The Provider component can be nested multiple levels deep within the component tree, but it should be placed at the highest level possible to minimize the number of unnecessary re-renders.
React Context solves the problem of having to pass props down through multiple levels of components, which can quickly become cumbersome and difficult to manage, especially in larger applications.
Some of the main problems that Context solves include:
By solving these problems, Context can make your React code more organized, maintainable, and performant.