You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1012 B
48 lines
1012 B
import {ThemeContext, themes} from './theme-context';
|
|
import ThemeTogglerButton from './theme-toggler-button';
|
|
|
|
class App extends React.Component {
|
|
constructor(props) {
|
|
super(props);
|
|
|
|
this.toggleTheme = () => {
|
|
this.setState(state => ({
|
|
theme:
|
|
state.theme === themes.dark
|
|
? themes.light
|
|
: themes.dark,
|
|
}));
|
|
};
|
|
|
|
// highlight-range{1-2,5}
|
|
// State also contains the updater function so it will
|
|
// be passed down into the context provider
|
|
this.state = {
|
|
theme: themes.light,
|
|
toggleTheme: this.toggleTheme,
|
|
};
|
|
}
|
|
|
|
render() {
|
|
// highlight-range{1,3}
|
|
// The entire state is passed to the provider
|
|
return (
|
|
<ThemeContext.Provider value={this.state}>
|
|
<Content />
|
|
</ThemeContext.Provider>
|
|
);
|
|
}
|
|
}
|
|
|
|
function Content() {
|
|
return (
|
|
<div>
|
|
<ThemeTogglerButton />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const root = ReactDOM.createRoot(
|
|
document.getElementById('root')
|
|
);
|
|
root.render(<App />);
|
|
|