forked from reactjs/ru.react.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme-detailed-app.js
More file actions
50 lines (45 loc) · 1.34 KB
/
theme-detailed-app.js
File metadata and controls
50 lines (45 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import {ThemeContext, themes} from './theme-context';
import ThemedButton from './themed-button';
// Промежуточный компонент, который использует ThemedButton
function Toolbar(props) {
return (
<ThemedButton onClick={props.changeTheme}>
Change Theme
</ThemedButton>
);
}
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
theme: themes.light,
};
this.toggleTheme = () => {
this.setState(state => ({
theme:
state.theme === themes.dark
? themes.light
: themes.dark,
}));
};
}
render() {
//highlight-range{1-4}
// ThemedButton внутри ThemeProvider использует
// значение светлой UI-темы из состояния, в то время как
// ThemedButton, который находится вне ThemeProvider,
// использует тёмную UI-тему из значения по умолчанию
//highlight-range{3-5,7}
return (
<Page>
<ThemeContext.Provider value={this.state.theme}>
<Toolbar changeTheme={this.toggleTheme} />
</ThemeContext.Provider>
<Section>
<ThemedButton />
</Section>
</Page>
);
}
}
ReactDOM.render(<App />, document.root);