mirror of
https://github.com/christianvidalwolf-prog/Craze-Data-check.git
synced 2026-08-03 10:55:25 +02:00
fix: resolve Uncaught TypeError: Cannot read properties of undefined (reading 'key') by ensuring usePersistentState handles functional updates safely
This commit is contained in:
@@ -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 setState = useCallback((value: T | ((prev: T) => T)) => {
|
||||
context.setState(key, value);
|
||||
}, [context, key]);
|
||||
context.setState(key, (current: any) => {
|
||||
const actualCurrent = current !== undefined ? current : defaultValue;
|
||||
return typeof value === 'function' ? (value as any)(actualCurrent) : value;
|
||||
});
|
||||
}, [context, key, defaultValue]);
|
||||
|
||||
return [state, setState];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user