fix: resolve Uncaught TypeError: Cannot read properties of undefined (reading 'key') by ensuring usePersistentState handles functional updates safely

This commit is contained in:
Christian Vidal Wolf
2026-04-24 20:31:32 +02:00
parent cd621de5d3
commit f0f78a6126
+5 -2
View File
@@ -33,8 +33,11 @@ export function usePersistentState<T>(key: string, defaultValue: T): [T, (value:
const state = context.states[key] !== undefined ? context.states[key] : defaultValue; const state = context.states[key] !== undefined ? context.states[key] : defaultValue;
const setState = useCallback((value: T | ((prev: T) => T)) => { const setState = useCallback((value: T | ((prev: T) => T)) => {
context.setState(key, value); context.setState(key, (current: any) => {
}, [context, key]); const actualCurrent = current !== undefined ? current : defaultValue;
return typeof value === 'function' ? (value as any)(actualCurrent) : value;
});
}, [context, key, defaultValue]);
return [state, setState]; return [state, setState];
} }