Skip to content

Commit

Permalink
Wrap the second parameter of the return value from useSetState with u…
Browse files Browse the repository at this point in the history
…seMemoriedFn. (#2592)

* style: change the default import to import on demand

* refactor: Wrap the second parameter of the return value from useSetState with useMemoriedFn
  • Loading branch information
askwuxue authored Sep 26, 2024
1 parent 73fa327 commit f58f34d
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/hooks/src/useSetState/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useCallback, useState } from 'react';
import { useState } from 'react';
import useMemoizedFn from '../useMemoizedFn';
import { isFunction } from '../utils';

export type SetState<S extends Record<string, any>> = <K extends keyof S>(
Expand All @@ -10,12 +11,12 @@ const useSetState = <S extends Record<string, any>>(
): [S, SetState<S>] => {
const [state, setState] = useState<S>(initialState);

const setMergeState = useCallback((patch) => {
const setMergeState = useMemoizedFn((patch) => {
setState((prevState) => {
const newState = isFunction(patch) ? patch(prevState) : patch;
return newState ? { ...prevState, ...newState } : prevState;
});
}, []);
});

return [state, setMergeState];
};
Expand Down

0 comments on commit f58f34d

Please sign in to comment.