35
loading...
This website collects cookies to deliver better user experience
The first thing you’ll want to do is to draw boxes around every component (and subcomponent) in the mock and give them all names.
const AwesomeComponent = (props) => {
const defaults = {
mode: 'dark',
};
const cache = {
mode: local.get('theme.mode'),
};
const initialTheme = merge(defaults, cache);
const [theme, setTheme] = useState(initialTheme);
return (
<ThemeContext.Provider value={{ theme, setTheme }}>
<div className="awesome-component">
<div>everything else...</div>
</div>
</ThemeContext.Provider>
);
};
useState
React hook. It also initializes the state with some default values taken from some options cached values.mode
. Let's make a function that initializes the state. This function will have a single purpose: initializing the state.const AwesomeComponent = (props) => {
const [theme, setTheme] = useState(themable());
return (
<ThemeContext.Provider value={{ theme, setTheme }}>
<div className="awesome-component">
<div>everything else...</div>
</div>
</ThemeContext.Provider>
);
};
const AwesomeComponent = (props) => {
return (
<Theme>
<div className="awesome-component">
<div>everything else...</div>
</div>
</Theme>
);
};