mirror of
https://github.com/christianvidalwolf-prog/Craze-Data-check.git
synced 2026-10-02 12:15:28 +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:
1 parent
cd621de5d3
commit
f0f78a6126
1 file changed
+5
-2
@@ -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