Skip to content

Commit

Permalink
fix(u, a-form): 解决提交表单后离开页面,表单还继续执行跳转的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
twinh committed Apr 25, 2022
1 parent 80b1a23 commit 5d05dcb
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions Form.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect } from 'react';
import { useEffect, useRef } from 'react';
import PropTypes from 'prop-types';
import curUrl from '@mxjs/cur-url';
import {Form as AntdForm} from 'antd';
Expand Down Expand Up @@ -67,6 +67,14 @@ const Form = (
) => {
const [form] = useAntdForm();
const history = useHistory();
const isMounted = useRef(false);

useEffect(() => {
isMounted.current = true;
return () => {
isMounted.current = false;
};
}, []);

// 默认自动从后台读取数据
useEffect(() => {
Expand Down Expand Up @@ -120,11 +128,11 @@ const Form = (
afterSubmit && afterSubmit(ret, form);

$.ret(ret).suc(() => {
if (!isMounted.current) {
return;
}
if (redirect) {
const url = getRedirectUrl(redirectUrl, ret);
if (url !== history.location.pathname) {
history.push(url);
}
history.push(getRedirectUrl(redirectUrl, ret));
}
});
});
Expand Down

0 comments on commit 5d05dcb

Please sign in to comment.