import { useCallback, useState } from 'react'; type Patch = Partial | ((previousState: T) => Partial); export function useSetState( initialState: T = {} as T, ): [T, (patch: Patch) => void] { const [state, set] = useState(initialState); const setState = useCallback((patch: Patch) => { set(previousState => ({ ...previousState, ...(patch instanceof Function ? patch(previousState) : patch), })); }, []); return [state, setState]; }