First, create a context in App.js.
Provide value using <UserContext.Provider>.
Use value of context using <UserContext.Consumer>.
Create context object and pass value in App.js as below.
- import React from 'react';
- import './App.css';
- import ParentComponent from './components/ParentComponent';
-
- export const UserContext = React.createContext()
-
- function App() {
- return (
- <div className="App">
- <UserContext.Provider value={'ReactApp'} >
- <ParentComponent/>
- </UserContext.Provider>
- </div>
- );
- }
-
- export default App;
and consume value in ChildC.js
- import React from 'react'
- import {UserContext} from '../App'
-
- function ChildC() {
- return (
- <div>
- Child Component at level 3
- <UserContext.Consumer>
- {
- user => {
- return <div>Context value is {user}</div>
- }
- }
- </UserContext.Consumer>
- </div>
- )
- }
-
- export default ChildC
The output will display as below.
Now, let’s see how multiple values can be consumed using context.
- import React from 'react';
- import './App.css';
- import ParentComponent from './components/ParentComponent';
-
- export const UserContext = React.createContext()
- export const CategoryContext = React.createContext()
-
- function App() {
- return (
- <div className="App">
- <UserContext.Provider value={'ReactApp'} >
- <CategoryContext.Provider value={'Programming'}>
- <ParentComponent />
- </CategoryContext.Provider>
- </UserContext.Provider>
- </div>
- );
- }
-
- export default App;
Now, import CategoryContext in ChildB.js.
- import React from 'react'
- import ChildC from './ChildC'
- import {CategoryContext} from '../App'
-
- function ChildB() {
- return (
- <div>
- <ChildC/>
- Child Component at level 2
- <CategoryContext.Consumer>
- {
- category => {
- return <div><b>Category is {category}</b></div>
- }
- }
- </CategoryContext.Consumer>
-
- </div>
- )
- }
-
- export default ChildB
The output will be displayed as below.
Now, we will import the CategoryContext in ChildC.js to see how nested consumer works.
- import React from 'react'
- import {UserContext,CategoryContext} from '../App'
-
- function ChildC() {
- return (
- <div>
- Child Component at level 3
- <UserContext.Consumer>
- {
- user => {
- return(
- <CategoryContext.Consumer>
- {
- category=>{
- return <div><b>Context value is {user} and Category value is {category}</b></div>
- }
- }
- </CategoryContext.Consumer>
-
- )
- }
- }
- </UserContext.Consumer>
- </div>
- )
- }
-
- export default ChildC
Now, it will display the output as below.
As in the above example, we have seen how to consume the context, we need to approach multi-level nesting, but it is very difficult to read and understand so to make it more readable, here, we will make use of the useContext() hook.
Let’s see a demo of useContext() to use context value in a more readable and understandable format,
The process of passing creating context and passing value remain the same; we just need to make changes while consuming values.
Update code in ChildB.js to consume context value.
- import React,{useContext} from 'react'
- import ChildC from './ChildC'
- import {UserContext,CategoryContext} from '../App'
-
- function ChildB() {
- const user = useContext(UserContext)
- const category = useContext(CategoryContext)
- return (
- <div>
- <ChildC/>
- Child Component at level 2
- <div><i>User is {user}, Category is {category}</i></div>
-
- </div>
- )
- }
-
- export default ChildB
As we can see in the above code, we just need to import useContext and after assigning the user.
Add Context and CategoryContext value to constant variables and we can simply use it as seen above.
The output is displayed in italics as below.
Using the useContext() hook makes code more readable and simpler to use.
Summary
In this article, we have learned about useContext() hook and how it works in React. Now in the next article we will learn about useReducer() hook.