日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

antd4中Form.create已廢棄

作者:馬優(yōu)晨 更新時(shí)間: 2022-02-25 編程語言

問題:

Warning: [antd: Form] antd v4 removed `Form.create`. Please remove or use `@ant-design/compatible` instead.

原因:

v4 的 Form 不再需要通過?Form.create()?創(chuàng)建上下文。Form 組件現(xiàn)在自帶數(shù)據(jù)域,因而?getFieldDecorator?也不再需要,直接寫入 Form.Item 即可

antd3方案:

// antd v3
const Demo = ({ form: { getFieldDecorator } }) => (
  <Form>
    <Form.Item>
      {getFieldDecorator('username', {
        rules: [{ required: true }],
      })(<Input />)}
    </Form.Item>
  </Form>
);
 
const WrappedDemo = Form.create()(Demo);

antd4方案:

getFieldDecorator類似于這些,都廢棄了。

// antd v4
const Demo = () => (
  <Form>
    <Form.Item name="username" rules={[{ required: true }]}>
      <Input />
    </Form.Item>
  </Form>
);

由于移除了 Form.create(),原本的 onFieldsChange 等方法移入 Form 中,通過 fields 對(duì) Form 進(jìn)行控制。

antd4從表單獲取數(shù)據(jù):

表單的數(shù)據(jù)函數(shù)都可以寫在Form里面,成功獲取onFinish,獲取失敗onFinishFailed

import { Form, Input, Button, Checkbox } from 'antd';

const Demo = () => {
  const onFinish = (values: any) => {
    console.log('Success:', values);
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log('Failed:', errorInfo);
  };

  return (
    <Form
      name="basic"
      labelCol={{ span: 8 }}
      wrapperCol={{ span: 16 }}
      initialValues={{ remember: true }}
      onFinish={onFinish}
      onFinishFailed={onFinishFailed}
      autoComplete="off"
    >
      <Form.Item
        label="Username"
        name="username"
        rules={[{ required: true, message: 'Please input your username!' }]}
      >
        <Input />
      </Form.Item>

      <Form.Item
        label="Password"
        name="password"
        rules={[{ required: true, message: 'Please input your password!' }]}
      >
        <Input.Password />
      </Form.Item>

      <Form.Item name="remember" valuePropName="checked" wrapperCol={{ offset: 8, span: 16 }}>
        <Checkbox>Remember me</Checkbox>
      </Form.Item>

      <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
        <Button type="primary" htmlType="submit">
          Submit
        </Button>
      </Form.Item>
    </Form>
  );
};

ReactDOM.render(<Demo />, mountNode);

原文鏈接:https://mayouchen.blog.csdn.net/article/details/122734059

欄目分類
最近更新