I am working on a React project.
In the following code, you can see that I am passing the same prop (theme: boolean value representing whether the app is currently toggled to dark mode or light mode) to every component. The reason I am passing theme to every component is because I use it to determine which mode I should style each component (using styled-components) in. This action of passing the same prop to every component seems redundant. How do I avoid the redundancy?
const App = () => {
const [theme, setTheme] = persist('theme', true);
return (
<React.StrictMode>
<div className='app'>
<Toggle theme={theme} setTheme={setTheme} />
<About theme={theme} />
<Links theme={theme} />
<Footer theme={theme} />
<Background theme={theme} />
</div>
</React.StrictMode>
);
};
Here is the code for the persist hook if it useful:
import React from 'react';
const persist = (key, defaultValue) => {
const [state, setState] = React.useState(() => {
const persistentState = localStorage.getItem(key);
return persistentState ? JSON.parse(persistentState) : defaultValue;
});
React.useEffect(() => {
window.localStorage.setItem(key, JSON.stringify(state));
}, [state, key]);
return [state, setState];
};
export default persist;