最近因为用抽屉组件会比较多,抽屉中有表单。每次关闭抽屉需要清除表单数据。所以自定义了一个方法
//reset form fields when Draw is form, closed function useResetFormOnCloseModal({ form, isShowDraw }: { form: any; isShowDraw: boolean }) { const prevVisibleRef = useRef(false); const prevVisible = prevVisibleRef.current; useEffect(() => { prevVisibleRef.current = isShowDraw; }, [isShowDraw]); useEffect(() => { if (!isShowDraw && prevVisible) { form.resetFields(); } }, [form, isShowDraw, prevVisible]); }
使用方式:
useResetFormOnCloseModal({ form, isShowDraw });