mirror of
https://github.com/christianvidalwolf-prog/Craze-Data-check.git
synced 2026-08-03 13:45:24 +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 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];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user