1. // 基于 ahooks useSetState 改造 https://github.com/alibaba/hooks/blob/master/packages/hooks/src/useSetState/index.ts
    2. import { useCallback, useState } from 'react';
    3. const useSetState = <T extends object>(
    4. initialState: T = {} as T,
    5. ): [T, (patch: Partial<T> | ((prevState: T) => Partial<T>)) => void, () => void] => {
    6. const [state, setState] = useState<T>(initialState);
    7. const setMergeState = useCallback(
    8. (patch) => {
    9. setState((prevState) => ({
    10. ...prevState,
    11. ...(patch instanceof Function ? patch(prevState) : patch),
    12. }));
    13. },
    14. [setState],
    15. );
    16. const resetState = useCallback(() => {
    17. setMergeState(initialState);
    18. }, [setState]);
    19. return [state, setMergeState, resetState];
    20. };
    21. export default useSetState;