Skip to content

Commit 3ccb373

Browse files
committed
chore(hooks): fix Maximum update depth exceeded
1 parent be04948 commit 3ccb373

2 files changed

Lines changed: 36 additions & 23 deletions

File tree

src/lib/hooks/useLocalStorage.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
1-
import { useState, useEffect } from 'react';
1+
import { useState, useEffect, useRef } from 'react';
22

33
export function useLocalStorage<T>(key: string, initialValue: T): [T, (value: T) => void] {
4+
45
const [storedValue, setStoredValue] = useState<T>(initialValue);
5-
const [isHydrated, setIsHydrated] = useState(false);
66

7-
7+
8+
const initialized = useRef(false);
9+
10+
811
useEffect(() => {
9-
setIsHydrated(true);
12+
if (typeof window === 'undefined' || initialized.current) return;
1013

1114
try {
1215
const item = localStorage.getItem(key);
13-
const parsedItem: T = item ? JSON.parse(item) : initialValue;
14-
setStoredValue(parsedItem);
16+
if (item) {
17+
setStoredValue(JSON.parse(item));
18+
}
19+
initialized.current = true;
1520
} catch (error) {
1621
console.error('Error reading from localStorage:', error);
17-
setStoredValue(initialValue);
1822
}
19-
}, [key, initialValue]);
23+
}, [key]);
24+
2025

21-
2226
const setValue = (value: T): void => {
2327
try {
24-
28+
2529
setStoredValue(value);
2630

27-
28-
if (isHydrated) {
31+
32+
if (typeof window !== 'undefined') {
2933
localStorage.setItem(key, JSON.stringify(value));
3034
}
3135
} catch (error) {

src/lib/theme/ThemeProvider.tsx

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface ThemeContextType {
1515
}
1616

1717
const ThemeContext = createContext<ThemeContextType>({
18-
isDarkTheme: true,
18+
isDarkTheme: false,
1919
toggleTheme: () => { },
2020
isLoaded: false,
2121
currentNetworkId: 'polkadot',
@@ -37,7 +37,9 @@ export function ThemeProvider({ children, initialNetworkId = 'polkadot' }: Theme
3737
const [isLoaded, setIsLoaded] = useState<boolean>(false);
3838
const [currentNetworkId, setCurrentNetworkId] = useState<string>(initialNetworkId);
3939
const [mounted, setMounted] = useState(false);
40+
const [storedThemeApplied, setStoredThemeApplied] = useState(false);
4041

42+
4143
const applyTheme = useCallback((isDark: boolean, networkId: string) => {
4244
if (typeof window === 'undefined') return;
4345

@@ -61,17 +63,21 @@ export function ThemeProvider({ children, initialNetworkId = 'polkadot' }: Theme
6163
});
6264
}, []);
6365

66+
6467
useEffect(() => {
65-
setMounted(true);
68+
if (typeof window === 'undefined' || storedThemeApplied) return;
6669

70+
setMounted(true);
6771
const storedTheme = localStorage.getItem('theme');
6872
const storedNetwork = localStorage.getItem('currentNetwork');
69-
const networkToUse = storedNetwork || currentNetworkId;
7073

74+
75+
const networkToUse = storedNetwork || initialNetworkId;
7176
if (storedNetwork && storedNetwork !== currentNetworkId) {
72-
setCurrentNetworkId(storedNetwork);
77+
setCurrentNetworkId(networkToUse);
7378
}
7479

80+
7581
let isDark: boolean;
7682
if (storedTheme) {
7783
isDark = storedTheme === 'dark';
@@ -82,7 +88,9 @@ export function ThemeProvider({ children, initialNetworkId = 'polkadot' }: Theme
8288
setIsDarkTheme(isDark);
8389
applyTheme(isDark, networkToUse);
8490
setIsLoaded(true);
91+
setStoredThemeApplied(true);
8592

93+
8694
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
8795
const handleChange = (e: MediaQueryListEvent) => {
8896
if (!localStorage.getItem('theme')) {
@@ -93,14 +101,15 @@ export function ThemeProvider({ children, initialNetworkId = 'polkadot' }: Theme
93101

94102
mediaQuery.addEventListener('change', handleChange);
95103
return () => mediaQuery.removeEventListener('change', handleChange);
96-
}, [applyTheme, currentNetworkId]);
104+
}, [applyTheme, initialNetworkId, storedThemeApplied]);
97105

106+
98107
useEffect(() => {
99-
if (isLoaded && mounted) {
100-
applyTheme(isDarkTheme, currentNetworkId);
101-
localStorage.setItem('currentNetwork', currentNetworkId);
102-
}
103-
}, [currentNetworkId, isLoaded, isDarkTheme, applyTheme, mounted]);
108+
if (!mounted) return;
109+
110+
applyTheme(isDarkTheme, currentNetworkId);
111+
localStorage.setItem('currentNetwork', currentNetworkId);
112+
}, [currentNetworkId, isDarkTheme, applyTheme, mounted]);
104113

105114
const toggleTheme = useCallback(() => {
106115
const newIsDark = !isDarkTheme;
@@ -148,7 +157,7 @@ export function ThemeProvider({ children, initialNetworkId = 'polkadot' }: Theme
148157

149158

150159
if (!mounted) {
151-
return null;
160+
return <div style={{ visibility: 'hidden' }}>{children}</div>;
152161
}
153162

154163
return (

0 commit comments

Comments
 (0)