📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials React Modern Development useContext

useContext

6 min read
useContext reads a shared value from a Context Provider without passing props through every level. Create context with createContext(), wrap with Provider, and read with useContext() in any child component.

Context and useContext

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

const ThemeContext = createContext("light");

function App() {
  const [theme, setTheme] = useState("light");
  return (
    
      
    
  );
}

function Button() {
  const { theme, setTheme } = useContext(ThemeContext);
  return (
    
  );
}