BeginnerCoder

Lesson 21 — Context

Share data without passing props

Prop drilling means passing a prop through many components that do not use it, just to reach a deep child. Context avoids it: createContext(defaultValue) makes a context, <Ctx.Provider value={…}> supplies a value to everything inside it, and useContext(Ctx) reads it in any child. Components outside every Provider get the default value.

const Lang = createContext("en");
function Hello() { return <p>{useContext(Lang) === "en" ? "Hi" : "Hola"}</p>; }

Why it matters: themes, the logged-in user and language settings usually live in context.

Want it another way?
Task

Read the theme with useContext. The preview should show "Theme: dark" (default) and "Theme: light" (inside the Provider).

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with React Foundations

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.