Skip to content
Learn Netverks

Lesson

Step 30/36 83% through track

usecontext

Context API

Last reviewed May 28, 2026 Content v20260528
Track mode
client_react
Means
In-browser React TSX
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Context API: the concepts, APIs, and habits you need before advancing in React.

Context shares cross-cutting data without prop drilling—but overuse creates opaque dependencies.

You will apply Context API in contexts like: Theme toggles, auth session, and feature flags shared across a component tree.

Write TypeScript/TSX, click Run in browser—React 18 loads from CDN, JSX compiles in the tab, UI renders in the preview root, and printOutput feeds the terminal.

Toward the end of the track—consolidate before capstone-style review lessons.

Context lets a provider pass a value to any descendant without drilling props through every layer. Common uses: theme, locale, authenticated user snapshot, feature flags.

API sketch

const ThemeContext = React.createContext('light');

function Provider({ children }) {
  const [theme, setTheme] = React.useState('light');
  return (
    <ThemeContext.Provider value={{ theme, setTheme }}>
      {children}
    </ThemeContext.Provider>
  );
}

function Panel() {
  const { theme } = React.useContext(ThemeContext);
}

Trade-offs

  • Consumers re-render when context value identity changes—split contexts or memoize value objects
  • Overuse hides data flow—reserve for truly cross-cutting concerns
  • Not a replacement for server cache libraries (React Query, etc.)

Self-check

  1. Why wrap the value in useMemo when providing objects?
  2. When is context better than prop drilling?

Pitfall: A new object literal in Provider value={{ ... }} every render re-renders all consumers—memoize the value object.

Interview prep

What is Context good for?

Cross-cutting values—theme, locale, auth snapshot—without drilling. Split contexts or memoize values to limit consumer re-renders.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs in your browser in a sandboxed frame. Backend runners appear when this track’s profile allows them.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Context value you shared?
  • Re-render concern?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump