BeginnerCoder

Lesson 23 — Tabs with context

Share state without passing props

Context lets a parent share a value with every component inside it, however deep, without passing props through each level. createContext() makes a context, <T.Provider value={...}> provides the value, and useContext(T) reads it. Here the Tabs parent shares [active, setActive], so every Tab and Panel stays in sync.

Note: Next.js pages are React components, so this preview runs your code with plain React in the browser. Next.js-only features (the file router, server rendering, data fetching on the server) are not running here — where a lesson needs them, it shows a small simulation instead.

Why it matters: themes, logged-in user and tab groups are all shared through context.

Want it another way?
Task

Finish Panel: read [active] from context and show children only when active === id.

← Back
Next →
Test your knowledge

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

Enjoying this lesson? Share it.

Keep going with Next.js 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.