React useContext Hook


React Context

React Context is a way of managing state globally.

It can be used in conjunction with the useState Hook to share status between nested sections easily rather than useState alone.


The Problem

The country should be kept at the highest parent level in the state that needs access to government.

To illustrate, we have many parts of the nest. The upper and lower part of the stack needs to reach the government.

In order to do this without Content, we will need to pass status as "props" on each nest site. This is called "prop drilling".


Example

Passing "props" through nested components:

import { useState } from "react";
            import ReactDOM from "react-dom";
            
            function Component1() {
              const [user, setUser] = useState("Jesse Hall");
            
              return (
                <>
                  <h1>{`Hello ${user}!`}</h1>
                  <Component2 user={user} />
                </>
              );
            }
            
            function Component2({ user }) {
              return (
                <>
                  <h1>Component 2</h1>
                  <Component3 user={user} />
                </>
              );
            }
            
            function Component3({ user }) {
              return (
                <>
                  <h1>Component 3</h1>
                  <Component4 user={user} />
                </>
              );
            }
            
            function Component4({ user }) {
              return (
                <>
                  <h1>Component 4</h1>
                  <Component5 user={user} />
                </>
              );
            }
            
            function Component5({ user }) {
              return (
                <>
                  <h1>Component 5</h1>
                  <h2>{`Hello ${user} again!`}</h2>
                </>
              );
            }
            
ReactDOM.render(<Component1 />, document.getElementById("root"));
            

Although sections 2-4 do not require a state, they had to transfer the state in order to reach section 5.


The Solution

The solution is to create context.

Create Context

To create a context, you must import createContext and launch it:


import { useState, createContext } from "react";
import ReactDOM from "react-dom";
            
const UserContext = createContext()
            

Next we will use the Content Provider to wrap a tree of parts that require a Content Status.

Context Provider

Wrap baby parts in the Content Provider and provide country value.


function Component1() {
            const [user, setUser] = useState("Jesse Hall");
          
            return (
              <UserContext.Provider value={user}>
                <h1>{`Hello ${user}!`}</h1>
                <Component2 user={user} />
              </UserContext.Provider>
            );
          }

Now, all parts of this tree will be able to access the user context.

Use the useContext Hook

In order to apply the Context on the child component, we need to access it using the useContext Hook.

First, enter useContext in the import statement:


import { useState, createContext, useContext } from "react";
        

You can then access User Content in all sections:


function Component5() {
            const user = useContext(UserContext);
          
            return (
              <>
                <h1>Component 5</h1>
                <h2>{`Hello ${user} again!`}</h2>
              </>
            );
          }
          


Full Example


Example

Here is the full example using React Context:

import { useState, createContext, useContext } from "react";
            import ReactDOM from "react-dom";
            
            const UserContext = createContext();
            
            function Component1() {
              const [user, setUser] = useState("Jesse Hall");
            
              return (
                <UserContext.Provider value={user}>
                  <h1>{`Hello ${user}!`}</h1>
                  <Component2 user={user} />
                </UserContext.Provider>
              );
            }
            
            function Component2() {
              return (
                <>
                  <h1>Component 2</h1>
                  <Component3 />
                </>
              );
            }
            
            function Component3() {
              return (
                <>
                  <h1>Component 3</h1>
                  <Component4 />
                </>
              );
            }
            
            function Component4() {
              return (
                <>
                  <h1>Component 4</h1>
                  <Component5 />
                </>
              );
            }
            
            function Component5() {
              const user = useContext(UserContext);
            
              return (
                <>
                  <h1>Component 5</h1>
                  <h2>{`Hello ${user} again!`}</h2>
                </>
              );
            }
            
ReactDOM.render(<Component1 />, document.getElementById("root"));