import { useDispatch } from '@@/plugin-dva/exports'; import { useEffect, useRef } from 'react'; export const useAutosize = (dependencies: any[]) => { const containerRef = useRef(null); const dispatch = useDispatch(); useEffect(() => { updateHeightWhenDomMounted(); }, dependencies); const updateHeightWhenDomMounted = () => { if (containerRef?.current) { const { parentElement } = containerRef.current || {}; const { clientHeight = 0 } = containerRef.current.children[0].children[0] || {}; if (clientHeight && parentElement) { updateHeight(clientHeight, parentElement.id); } } }; const updateHeight = (height: number, id: string) => { dispatch({ type: 'editorModal/updateH', payload: { h: height / 2, id: id, }, }); }; return { containerRef, updateHeightWhenDomMounted, updateHeight }; };