SkillPilotAI

Context

Context lets a value reach any component deep in the tree without passing it through props at every level. It solves prop drilling for shared data like the theme, the logged-in user or the language, but every consumer re-renders when the value changes.

35.1 The problem: prop drilling

  • App knows the user Priya and passes it to Navbar as a prop.
  • Navbar does not use it, only passes it to Menu; Menu only passes it to Avatar.
  • Only Avatar actually shows the name. The output is correct, but the middle components became couriers.
  • In a real app, a user or theme needed ten levels down loads every component in between with props for no reason.
React (JSX)
function App() {  return <Navbar user="Priya" />;} function Navbar({ user }) {  return <Menu user={user} />;} function Menu({ user }) {  return <Avatar user={user} />;} function Avatar({ user }) {  return <p>Hi, {user}</p>;}
Output
Hi, Priya
Prop drilling
  1. 1.App: has user="Priya"

  2. 2.Navbar: does not use it, only passes it

  3. 3.Menu: does not use it, only passes it

  4. 4.Avatar: actually uses it -> Hi, Priya

Middle components become couriers

35.2 Step 1: createContext

  • Context is a built-in React feature to deliver a value down the tree without props.
  • createContext("Guest") creates UserContext; "Guest" is the default value, used only when there is no provider above.
JavaScript
import { createContext } from "react"; export const UserContext =  createContext("Guest");

35.3 Step 2: the provider

  • Wrap Navbar in UserContext and give value="Priya": this is the provider.
  • It provides the value to the whole tree inside it, however deep a component is.
  • In React 19 the context itself works as the provider; older code uses UserContext.Provider (same job).
  • Navbar and Menu now take no props.
React (JSX)
import { UserContext } from "./UserContext";import Navbar from "./Navbar"; export default function App() {  return (    <UserContext value="Priya">      <Navbar />    </UserContext>  );}

35.4 Step 3: useContext

  • Avatar calls useContext(UserContext); React looks up the tree for the nearest provider and returns its value.
  • Here that is Priya, so the screen shows Hi, Priya without passing anything through Navbar or Menu.
React (JSX)
import { useContext } from "react";import { UserContext } from "./UserContext"; export default function Avatar() {  const user = useContext(UserContext);  return <p>Hi, {user}</p>;}
Output
Hi, Priya
Context in three steps
  1. 1.createContext("Guest"): context created with a default

  2. 2.<UserContext value="Priya">: provider high in the tree

  3. 3.Avatar deep inside calls useContext(UserContext)

  4. 4.React walks up to the nearest provider

  5. 5.Returns "Priya" (or "Guest" if no provider)

No provider? Default value

35.5 Checkpoint: nearest provider vs default

  • The first Avatar is inside the provider and gets Priya.
  • The second Avatar has no provider above it, so useContext returns the default Guest.
React (JSX)
import { UserContext } from "./UserContext";import Avatar from "./Avatar"; export default function App() {  return (    <>      <UserContext value="Priya">        <Avatar />      </UserContext>      <Avatar />    </>  );}
Output
Hi, PriyaHi, Guest

35.6 Provider pattern: ThemeProvider

  • Put the context inside its own provider component: ThemeProvider holds a dark state and a toggle function.
  • The value is one object { dark, toggle } wrapping children: state lives in one place but is available everywhere.
  • ThemeButton reads the theme with useContext; clicking calls theme.toggle and the text flips from Light mode to Dark mode, however deep the button is.
  • The same pattern is used for auth: an AuthProvider gives the logged-in user plus login and logout functions.
React (JSX)
import { useState } from "react";import { ThemeContext } from "./ThemeContext"; export function ThemeProvider({ children }) {  const [dark, setDark] = useState(false);  const toggle = () => setDark((d) => !d);   return (    <ThemeContext value={{ dark, toggle }}>      {children}    </ThemeContext>  );}
React (JSX)
import { useContext } from "react";import { ThemeContext } from "./ThemeContext"; export function ThemeButton() {  const theme = useContext(ThemeContext);  return (    <button onClick={theme.toggle}>      {theme.dark ? "Dark" : "Light"} mode    </button>  );}
Output
[button] Light mode-- click --[button] Dark mode

35.7 Re-render cost: one big context

  • When a provider's value changes, every component that uses that context re-renders.
  • Wrong: user and cart in one AppContext; adding an item to the cart also re-renders Avatar, which only needed the user.
  • Fix: split contexts (UserContext and CartContext) so each component re-renders only when its own value changes.
React (JSX)
// Wrong: one big context -> cart changes re-render Avatar tooexport function Providers({ children }) {  const [user] = useState("Priya");  const [cart, setCart] = useState([]);  return (    <AppContext value={{ user, cart, setCart }}>      {children}    </AppContext>  );}
React (JSX)
// Right: split contexts -> cart changes do not touch Avatarexport function Providers({ children }) {  const [user] = useState("Priya");  const [cart, setCart] = useState([]);  return (    <UserContext value={user}>      <CartContext value={{ cart, setCart }}>        {children}      </CartContext>    </UserContext>  );}

35.8 Comparison: props vs Context

PointPropsContext
How data travelsParent to direct child, level by levelProvider to any consumer below, directly
Best forData a few levels downData many components need (theme, user, language)
Re-renderChild re-renders with parentEvery consumer re-renders when the value changes
Default valueNoneValue passed to createContext

35.9 Common mistakes

  • Putting unrelated data into one big context: unnecessary re-renders of every consumer.
  • Using context for every small prop instead of only truly shared data.
  • Myth: "Context is a state manager / Redux replacement". Context only delivers a value; the state still lives in useState or useReducer. Think of it as transport, not storage.
  • For data that changes very frequently, a dedicated store works better (covered later in the course).

35.10 Best practices

  • Use context only for data many components need: theme, logged-in user, language.
  • Keep the provider only as high as needed; split a big context into smaller ones.
  • Create a custom hook like useTheme for consumers that throws a clear error when the provider is missing.

35.11 Interview tips

  • Context solves prop drilling: a value reaches any component down the tree without props.
  • createContext creates it, the provider gives the value, useContext reads the nearest provider's value; no provider means the default value.
  • Mention the re-render cost: every consumer re-renders when the value changes, which is why we split contexts.
  • Context is not a state manager; pair it with useState/useReducer.