Creating and Using Context

The Context API in React provides a way to pass data through the component tree without having to pass props down manually at every level.

import React, { createContext, useContext } from 'react';

const UserContext = createContext();

function App() {
  return (
    
      
    
  );
}

function Profile() {
  const user = useContext(UserContext);
  return 

Hello, {user.name}

; }

createContext sets up the context, Provider shares the data, and useContext accesses it in child components.

← PrevNext →