From 9c254b679ae3d35416d18724a7c15d57819e1905 Mon Sep 17 00:00:00 2001 From: Swain Molster Date: Tue, 7 Nov 2023 02:05:18 -0500 Subject: [PATCH] fix: correct behavior of function setState parameter --- src/hooks.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/hooks.ts b/src/hooks.ts index b7d3b7db..53cdc5fe 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -59,13 +59,11 @@ function createMMKVHook< ): [value: T, setValue: (value: TSetAction) => void] => { const mmkv = instance ?? getDefaultInstance(); const [value, setValue] = useState(() => getter(mmkv, key)); - const valueRef = useRef(value); - valueRef.current = value; // update value by user set const set = useCallback( (v: TSetAction) => { - const newValue = typeof v === 'function' ? v(valueRef.current) : v; + const newValue = typeof v === 'function' ? v(getter(mmkv, key)) : v; switch (typeof newValue) { case 'number': case 'string':