fix: 🐛 ThemeToggle should initialize from localstorage immediately (#748)
Co-authored-by: Tony Sullivan <tony.f.sullivan@gmail.com>
This commit is contained in:
parent
054e79acd7
commit
7f57651686
1 changed files with 6 additions and 6 deletions
|
@ -43,13 +43,13 @@ const icons = [
|
|||
];
|
||||
|
||||
const ThemeToggle: FunctionalComponent = () => {
|
||||
const [theme, setTheme] = useState(themes[0]);
|
||||
const [theme, setTheme] = useState(() => {
|
||||
if (typeof localStorage === 'undefined') {
|
||||
return themes[0];
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
const user = localStorage.getItem('theme');
|
||||
if (!user) return;
|
||||
setTheme(user);
|
||||
}, []);
|
||||
return localStorage.getItem('theme') || themes[0];
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
const root = document.documentElement;
|
||||
|
|
Loading…
Reference in a new issue