Skip to content

Commit

Permalink
🎬 fix Form demo warning (#21576)
Browse files Browse the repository at this point in the history
close #21543
  • Loading branch information
afc163 committed Feb 25, 2020
1 parent 30f8368 commit 2f91dd4
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions components/form/demo/form-context.md
Expand Up @@ -14,7 +14,7 @@ title:
Use `Form.Provider` to process data between forms. In this case, submit button is in the Modal which is out of Form. You can use `form.submit` to submit form. Besides, we recommend native `<Button htmlType="submit" />` to submit a form.

```tsx
import React, { useState, useEffect } from 'react';
import React, { useState, useEffect, useRef } from 'react';
import { Form, Input, InputNumber, Modal, Button, Avatar, Typography } from 'antd';
import { SmileOutlined, UserOutlined } from '@ant-design/icons';

Expand All @@ -34,8 +34,17 @@ interface ModalFormProps {
const ModalForm: React.FC<ModalFormProps> = ({ visible, onCancel }) => {
const [form] = Form.useForm();

const prevVisibleRef = useRef();
useEffect(() => {
form.resetFields();
prevVisibleRef.current = visible;
}, [visible]);
const prevVisible = prevVisibleRef.current;

useEffect(() => {
console.log(visible, prevVisible);
if (!visible && prevVisible) {
form.resetFields();
}
}, [visible]);

const onOk = () => {
Expand Down

2 comments on commit 2f91dd4

@kothing
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

@kothing
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useEffect(() => { prevVisibleRef.current = visible; }, [visible]);

Please sign in to comment.