import useMergedState from 'rc-util/lib/hooks/useMergedState'; import { useEffect, useRef } from 'react'; 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); const frame = useRef(0); useEffect(() => { mountRef.current = true; return () => { mountRef.current = false; }; }); const [state, setState] = useMergedState(initialState, option); const mountSetState: Dispatch = (prevState: S) => { cancelAnimationFrame(frame.current); frame.current = requestAnimationFrame(() => { if (mountRef.current) { setState(prevState); } }); }; return [state, mountSetState]; } export default useMountMergeState;