diff --git a/packages/hooks/src/useSetState/index.ts b/packages/hooks/src/useSetState/index.ts index dc031a6ea8..495bcfd243 100644 --- a/packages/hooks/src/useSetState/index.ts +++ b/packages/hooks/src/useSetState/index.ts @@ -1,13 +1,20 @@ import { useCallback, useState } from 'react'; import { isFunction } from '../utils'; -const useSetState = >( - initialState: T = {} as T, -): [T, (patch: Partial | ((prevState: T) => Partial)) => void] => { - const [state, setState] = useState(initialState); +export type SetState> = ( + state: Pick | null | ((prevState: Readonly) => Pick | S | null), +) => void; + +const useSetState = >( + initialState: S | (() => S), +): [S, SetState] => { + const [state, setState] = useState(initialState); const setMergeState = useCallback((patch) => { - setState((prevState) => ({ ...prevState, ...(isFunction(patch) ? patch(prevState) : patch) })); + setState((prevState) => { + const newState = isFunction(patch) ? patch(prevState) : patch; + return newState ? { ...prevState, ...newState } : prevState; + }); }, []); return [state, setMergeState];