|
1 |
| -import React, {Component} from 'react'; |
| 1 | +import React, {useState, Suspense} from 'react'; |
2 | 2 |
|
3 | 3 | import Chrome from './Chrome';
|
4 | 4 | import Page from './Page';
|
5 | 5 |
|
6 |
| -export default class App extends Component { |
7 |
| - render() { |
8 |
| - return ( |
9 |
| - <Chrome title="Hello World" assets={this.props.assets}> |
| 6 | +function LoadingIndicator({theme}) { |
| 7 | + return <div className={theme + '-loading'}>Loading...</div>; |
| 8 | +} |
| 9 | + |
| 10 | +function ThemeToggleButton({theme, onChange}) { |
| 11 | + let [targetTheme, setTargetTheme] = useState(theme); |
| 12 | + function toggleTheme() { |
| 13 | + let newTheme = theme === 'light' ? 'dark' : 'light'; |
| 14 | + // High pri, responsive update. |
| 15 | + setTargetTheme(newTheme); |
| 16 | + // Perform the actual theme change in a separate update. |
| 17 | + setTimeout(() => onChange(newTheme), 0); |
| 18 | + } |
| 19 | + if (targetTheme !== theme) { |
| 20 | + return 'Switching to ' + targetTheme + '...'; |
| 21 | + } |
| 22 | + return ( |
| 23 | + <button onClick={toggleTheme}> |
| 24 | + Switch to {theme === 'light' ? 'Dark' : 'Light'} theme |
| 25 | + </button> |
| 26 | + ); |
| 27 | +} |
| 28 | + |
| 29 | +export default function App({assets}) { |
| 30 | + let [theme, setTheme] = useState('light'); |
| 31 | + return ( |
| 32 | + <Chrome title="Hello World" assets={assets} theme={theme}> |
| 33 | + <div> |
| 34 | + <h1>Hello World</h1> |
10 | 35 | <div>
|
11 |
| - <h1>Hello World</h1> |
12 |
| - <Page /> |
| 36 | + <ThemeToggleButton theme={theme} onChange={setTheme} /> |
13 | 37 | </div>
|
14 |
| - </Chrome> |
15 |
| - ); |
16 |
| - } |
| 38 | + <Suspense fallback={<LoadingIndicator theme={theme} />}> |
| 39 | + <Page theme={theme} /> |
| 40 | + </Suspense> |
| 41 | + </div> |
| 42 | + </Chrome> |
| 43 | + ); |
17 | 44 | }
|
0 commit comments