React.createContext() is a React API for creating context objects. It allows you to pass data through the component tree without having to pass props down manually at every level. It is mainly used when some data needs to be accessible by many components at different nesting levels.
For example, consider a React app that has a context of a user, which includes information such as the user's name, email, and age. This context can be passed down through the component tree, allowing any component to access the data without needing to pass props manually.
xxxxxxxxxx
// Create the context
const UserContext = React.createContext();
// Provide a context value
const App = () => (
<UserContext.Provider value={{ name: 'John', email: 'john@example.com', age: 32 }}>
<Layout />
</UserContext.Provider>
);
// Access the context value
const Layout = () => (
<UserContext.Consumer>
{(user) => (
<div>
<p>Name: {user.name}</p>
<p>Email: {user.email}</p>
<p>Age: {user.age}</p>
</div>
)}
</UserContext.Consumer>
);
export default App;
xxxxxxxxxx
// ---------------- Parent.js
import {createContext , useState} from "react";
let contextName = createContext( ) // you can optionally pass it a default value // it returns a "provider" object
let Parent = () => {
let [count , setCount] = useState() // returns an [ state variable , state setter fn]. you can pass useState() a default value too
// the stateSetter function is async
return (
<div>
<contextName.Provider value={ {count , setCount } } > // you can send multiple things across to the consumers by adding them in a single object
<B/>
</contextName.Provider>
</div>
)
}
export {contextName};
export default Parent;
// file that consumes the context
// --------- Child.js -----------
import {useContext} from "react"; // import the useContext hook that will allow you to actually use the created and imported context
import {contextName } from "./Parent"; // import the particular context that you want to use
let Child = () => {
let value = useContext(contextName);
return (
<div> {value} </div>
)
}
xxxxxxxxxx
import { createContext } from 'react';
const NameContext = createContext('');
function App() {
return (
<NameContext.Provider value="John Doe">
<Body />
<NameContext.Provider>
);
}
function Body() {
return <Greeting />;
}
function Greeting() {
return (
<NameContext.Consumer>
{name => <h1>Welcome, {name}</h1>}
</NameContext.Consumer>
);
}
xxxxxxxxxx
//React context allows us to pass data to our component tree without using props.
To use Context, we use the createContext function from React.
The created context includes a Provider and a Consumer property, which are each components.
We wrap the Provider around the component tree that we want to pass
the given value down.
Next, we place the Consumer in the component we want to consume the value.
import { createContext } from 'react';
const NameContext = createContext('');
function App() {
return (
<NameContext.Provider value="John Doe">
<Body />
<NameContext.Provider>
);
}
function Body() {
return <Greeting />;
}
function Greeting() {
return (
<NameContext.Consumer>
{name => <h1>Welcome, {name}</h1>}
//we got function with name argument which we will use
</NameContext.Consumer>
);
}
//using useContext hook
rewrite our example from earlier, using the useContext hook:
import { createContext, useContext } from 'react';
const NameContext = createContext('');
function App() {
return (
<NameContext.Provider value="John Doe">
<Body />
<NameContext.Provider>
);
}
function Body() {
return <Greeting />;
}
function Greeting() {
const name = useContext(NameContext);
//useContext with name of context
return (
<h1>Welcome, {name}</h1>
);
}