fix: 🐛 ThemeToggle should initialize from localstorage immediately (#748)

Co-authored-by: Tony Sullivan <tony.f.sullivan@gmail.com>
This commit is contained in:
Tony Sullivan 2021-07-20 01:18:02 +02:00 committed by GitHub
parent 054e79acd7
commit 7f57651686
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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;