MCPcopy
hub / github.com/facebook/react / ThemeToggleButton

Function ThemeToggleButton

fixtures/ssr/src/components/Theme.js:7–25  ·  view source on GitHub ↗
({onChange})

Source from the content-addressed store, hash-verified

5export default Theme;
6
7export function ThemeToggleButton({onChange}) {
8 let theme = useContext(Theme);
9 let [targetTheme, setTargetTheme] = useState(theme);
10 function toggleTheme() {
11 let newTheme = theme === 'light' ? 'dark' : 'light';
12 // High pri, responsive update.
13 setTargetTheme(newTheme);
14 // Perform the actual theme change in a separate update.
15 setTimeout(() => onChange(newTheme), 0);
16 }
17 if (targetTheme !== theme) {
18 return 'Switching to ' + targetTheme + '...';
19 }
20 return (
21 <a className="link" onClick={toggleTheme}>
22 Switch to {theme === 'light' ? 'Dark' : 'Light'} theme
23 </a>
24 );
25}

Callers

nothing calls this directly

Calls 2

useContextFunction · 0.90
useStateFunction · 0.90

Tested by

no test coverage detected