import { useRef, useEffect } from 'react'; interface IRecycleTarget { onMount: () => void; onUnmount: () => void; } export const useAttach = (target: T): T => { const oldTargetRef = useRef(null); useEffect(() => { if (oldTargetRef.current && target !== oldTargetRef.current) { oldTargetRef.current.onUnmount(); } oldTargetRef.current = target; target.onMount(); return () => { target.onUnmount(); }; }, [target]); return target; };