Context API

 1 We use context API to provide context which is available to all the components of the App directly.


2 We use a Context Provider that wraps the App.js which means it wraps the whole components. So that the context can be made available to all the components.


check link:   https://youtu.be/aAcI_FdfkA8

Eg: In UserContext file located in context in src of React Practises;
Create a constant which assigns to React,createContext.

Create a file named Context Provider that provides the context to the Usercontext

Syntax of Context Provider:

import React, { useState } from "react";
import UserContext from "./UserContext";

const UserContextProvider = ({ children }) => {
  const [user, setUser] = useState(null);
  return(
    <UserContext.Provider>
        {children}
    </UserContext.Provider>
  )
};
export default UserContextProvider;






Comments