【react antd】关闭抽屉重置表单数据

最近因为用抽屉组件会比较多,抽屉中有表单。每次关闭抽屉需要清除表单数据。所以自定义了一个方法

//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
    });

  

 

posted @ 2020-07-30 16:47  文学少女  阅读(1164)  评论(0编辑  收藏  举报