hardFrontend

What changed with Context in React 19 (`<Context>` as a provider)?

62 views
01

Understand the problem

Rendering Context directly as a provider and reading it with use().

reactreact-19context
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Stuck? AI Nudge Available

Get a conceptual hint to guide your logic without spoiling the final implementation.

03

Study the solution

The solution is waiting

Give it an honest attempt first — then compare your thinking with the full walkthrough.

04

Explore the playground snippets

<Context> as provider + use(Context)
Run Playground
import { createContext, use, useState } from 'react';

const ThemeContext = createContext('light');

function Banner() {
  const theme = use(ThemeContext);   // React 19: use() reads context
  const dark = theme === 'dark';
  return (
    <div style={{ padding: 16, borderRadius: 8, background: dark ? '#111' : '#eee', color: dark ? '#fff' : '#111' }}>
      Current theme: {theme}
    </div>
  );
}

export default function App() {
  const [theme, setTheme] = useState('light');
  return (
    // React 19: render <Context> directly as the provider (no .Provider)
    <ThemeContext value={theme}>
      <div style={{ fontFamily: 'sans-serif', padding: 24 }}>
        <Banner />
        <button style={{ marginTop: 8 }} onClick={() => setTheme((t) => (t === 'light' ? 'dark' : 'light'))}>
          Toggle theme
        </button>
      </div>
    </ThemeContext>
  );
}
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.