Share a theme with every button inside a Provider, without passing props through Toolbar.
React · beginner · 7 min · 26 steps · free
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>
);
}<App> for the first time: it calls the App function.useState runs for the first time, so React stores the starting value for theme and returns it: "light".<ThemeContext.Provider value={theme}> makes theme available to every component inside it, however deep.Toolbar returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.<Button> for the first time: it calls the Button function.useContext(ThemeContext) finds the nearest Provider above: theme = "light".Button returns JSX: a description of what the screen should show, <button className="light">Save (light)</button>. React has not changed the page yet.<Button> for the first time: it calls the Button function.useContext reads the value from the nearest Provider above: theme = "light".Button returns JSX: a description of what the screen should show, <button className="light">Cancel (light)</button>. React has not changed the page yet.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".<App> again (render 2): it calls App once more, from the top.useState returns the value React has kept for theme: "dark". The starting value is only used on the first render.App returns JSX: a description of what the screen should show, <Provider value="dark">…</Provider>. React has not changed the page yet.<Toolbar> again (render 2): it calls Toolbar once more, from the top.Toolbar returns JSX: a description of what the screen should show, <div>…</div>. React has not changed the page yet.<Button> again (render 2): it calls Button once more, from the top.Button returns JSX: a description of what the screen should show, <button className="dark">Save (dark)</button>. React has not changed the page yet.<Button> again (render 2): it calls Button once more, from the top.useContext reads the value from the nearest Provider above: theme = "dark".Button returns JSX: a description of what the screen should show, <button className="dark">Cancel (dark)</button>. React has not changed the page yet.Answer: The default from createContext
Answer: Components that read the context
← useRef: Values That Don't Re-render · useReducer: State Logic in One Place →