Skip to content

Commit

Permalink
fix: Fix correct behavior of function setState parameter (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
swain authored Nov 7, 2023
1 parent 6327d85 commit f391f9b
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(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':
Expand Down

0 comments on commit f391f9b

Please sign in to comment.