I've just created a theme handler with React.js.
Could my Theme
component be polished and improved a bit ?
function Theme() {
const [checkbox, setCheckbox] = React.useState(
JSON.parse(localStorage.getItem("configuration"))?.dark_theme ?? true
);
React.useEffect(() => (
!checkbox
? document.body.setAttribute('data-theme', 'light')
: document.body.removeAttribute('data-theme'),
setToLocalStorage({ dark_theme: checkbox }, "configuration") //memorization
), [checkbox])
return (
<div className="theme">
☀️
<label htmlFor="theme-checkbox">
<input
type="checkbox"
id="theme-checkbox"
defaultChecked={checkbox}
onChange={() => setCheckbox(!checkbox)}
/>
<span className="slidebar"></span>
</label>
🌒
</div>
)
}
Thanks for your help