import { useRef, useEffect, useCallback } from 'react'; import useMergedState from 'rc-util/lib/hooks/useMergedState'; type Dispatch = (value: A) => void; function useMountMergeState( initialState: S | (() => S), option?: { defaultValue?: S; value?: S; onChange?: (value: S, prevValue: S) => void; postState?: (value: S) => S; }, ): [S, Dispatch] { const mountRef = useRef(false); useEffect(() => { mountRef.current = true; return () => { mountRef.current = false; }; }); const [state, setState] = useMergedState(initialState, option); const mountSetState: Dispatch = useCallback( (prevState: S) => { requestAnimationFrame(() => { if (mountRef.current) { setState(prevState); } }); }, [setState], ); return [state, mountSetState]; } export default useMountMergeState;