Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    ⚛React

    Components and JSX

    • Why React?
    • JSX rules
    • Props
    • Rendering lists and keys
    • Conditional rendering

    State and events

    • Handling events
    • useState: a component's memory
    • Objects and arrays in state
    • Forms and controlled inputs
    • Lifting state up

    Effects and essential hooks

    • useEffect
    • Fetching data
    • useRef
    • Context: data for the whole tree
    • useReducer

    Professional React

    • Writing your own hooks
    • Performance: memo, useMemo and useCallback
    • Suspense and error boundaries
    • What's new in React 19
    • Thinking in React
    • Project: a complete to-do app
    React

    Effects and essential hooks · Lesson 14 of 21

    Context: data for the whole tree

    createContext, Provider and useContext, and the end of prop drilling.

    9 minutes2 games

    When data like the theme, the current user or the language is needed by many components in different places, passing it as props from parent to child to grandchild is tedious: prop drilling. Context lets any component below a Provider read the value directly.

    React
    const ThemeContext = createContext("light")
    
    function Button({ children }) {
      const theme = useContext(ThemeContext)
      const dark = theme === "dark"
      return (
        <button style={{ background: dark ? "#111" : "#eee", color: dark ? "#fff" : "#111", padding: "6px 12px", borderRadius: 8 }}>
          {children}
        </button>
      )
    }
    
    function Toolbar() {
      return <div><Button>Save</Button> <Button>Share</Button></div>    // no theme prop here
    }
    
    function App() {
      const [theme, setTheme] = useState("light")
      return (
        <ThemeContext.Provider value={theme}>
          <label>
            <input type="checkbox" checked={theme === "dark"} onChange={(e) => setTheme(e.target.checked ? "dark" : "light")} /> Dark
          </label>
          <Toolbar />
        </ThemeContext.Provider>
      )
    }
    • ThemeContext.Provider value="dark"
      • Toolbar (doesn't touch theme)
        • Button → useContext → "dark"
        • Button → useContext → "dark"

    Toolbar knows nothing about the theme; the buttons reach it directly.

    Important

    Whenever the Provider's value changes, every component calling useContext re-renders. So Context suits rarely-changing things (theme, language, user). If you pass a new object each render (value={{ user, setUser }}), wrap it in useMemo or split it into two contexts.

    SeaCode itself does this: AuthProvider wraps the app, and any page calls useAuth() (a custom hook wrapping useContext) to know who's signed in.

    What's the output?

    const Lang = createContext("ar")
    
    function Label() {
      return <span>{useContext(Lang)}</span>
    }
    
    function App() {
      return (
        <div>
          <Label />
          <Lang.Provider value="en">
            <Label />
            <Lang.Provider value="fr">
              <Label />
            </Lang.Provider>
          </Lang.Provider>
        </div>
      )
    }

    What will the page show?

    Interview question

    Is Context a replacement for Redux or Zustand?

    Exercise

    Create a UserContext; App wraps everything in a Provider with { name: "Nour", xp: 410 }. The Navbar component (no props) shows Nour · 410 xp.

    The page should show: Nour · 410 xp

    React
    function Navbar() {
      return <nav>Guest</nav>
    }
    
    function App() {
      return <Navbar />
    }

    Learn more from

    • react.dev: Passing data deeply with contextreact.dev
    • react.dev: useContext referencereact.dev
    • react.dev: Scaling up with reducer and contextreact.dev
    • Kent C. Dodds: How to use React Context effectivelykentcdodds.com

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PrevioususeRefNextuseReducer
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.