useContext: Passing Data Deeply

Share a theme with every button inside a Provider, without passing props through Toolbar.

React · beginner · 7 min · 26 steps · free

What you'll learn

UseContext.jsx

import { createContext, useContext, useState } from 'react';

const ThemeContext = createContext('light');

function Button({ children }) {
  const theme = useContext(ThemeContext);
  return <button className={theme}>{children} ({theme})</button>;
}

function Toolbar() {
  return (
    <div>
      <Button>Save</Button>
      <Button>Cancel</Button>
    </div>
  );
}

export default function App() {
  const [theme, setTheme] = useState('light');

  return (
    <ThemeContext.Provider value={theme}>
      <Toolbar />
      <label>
        <input type="checkbox" onChange={(e) => setTheme(e.target.checked ? 'dark' : 'light')} />
        Dark mode
      </label>
    </ThemeContext.Provider>
  );
}

Step by step

  1. React renders <App> for the first time: it calls the App function.
  2. useState runs for the first time, so React stores the starting value for theme and returns it: "light".
  3. <ThemeContext.Provider value={theme}> makes theme available to every component inside it, however deep.
  4. Toolbar takes no props and passes no theme. It does not need to.
  5. Toolbar returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.
  6. React renders <Button> for the first time: it calls the Button function.
  7. useContext(ThemeContext) finds the nearest Provider above: theme = "light".
  8. Button returns JSX: a description of what the screen should show, <button className="light">Save (light)</button>. React has not changed the page yet.
  9. React renders <Button> for the first time: it calls the Button function.
  10. useContext reads the value from the nearest Provider above: theme = "light".
  11. Button returns JSX: a description of what the screen should show, <button className="light">Cancel (light)</button>. React has not changed the page yet.
  12. React turns the JSX into real page elements and shows them: look at the Screen panel.
  13. You tick Dark mode.
  14. setTheme(e.target.checked ? 'dark' : 'light') does not change theme right away: React queues the update and will render again. In this call theme is still "light".
  15. React renders <App> again (render 2): it calls App once more, from the top.
  16. useState returns the value React has kept for theme: "dark". The starting value is only used on the first render.
  17. App returns JSX: a description of what the screen should show, <Provider value="dark">…</Provider>. React has not changed the page yet.
  18. React renders <Toolbar> again (render 2): it calls Toolbar once more, from the top.
  19. Toolbar returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.
  20. React renders <Button> again (render 2): it calls Button once more, from the top.
  21. Button reads the context again.
  22. Button returns JSX: a description of what the screen should show, <button className="dark">Save (dark)</button>. React has not changed the page yet.
  23. React renders <Button> again (render 2): it calls Button once more, from the top.
  24. useContext reads the value from the nearest Provider above: theme = "dark".
  25. Button returns JSX: a description of what the screen should show, <button className="dark">Cancel (dark)</button>. React has not changed the page yet.
  26. Both buttons now say dark.

Common mistakes

Best practices

Key takeaways

Check yourself

No Provider above. useContext returns?

Answer: The default from createContext

Provider value changes. Who re-renders?

Answer: Components that read the context

·

More in Sharing State