简体   繁体   English

如何重置表单当我单击提交按钮时,我使用 React 钩子做出反应

[英]How to Reset a form When I click submit Button I React using React hooks

I am working on a React project.我正在做一个 React 项目。 In my project I have a form after entering all input fields when I click the submit then I have reset a form.在我的项目中,当我单击提交时输入所有输入字段后,我有一个表单,然后我重置了一个表单。 But when I am trying to achieve this I am getting error like this TypeError:但是当我试图实现这一点时,我收到了类似 TypeError 的错误:

antd_es_form__WEBPACK_IMPORTED_MODULE_8__.default.useForm is not a function antd_es_form__WEBPACK_IMPORTED_MODULE_8__.default.useForm 不是函数

This is my code这是我的代码

import React, { useState } from "react";
import { Row, Col, Button, Card, Form, Input, Select } from "antd";
import axios from 'axios'
import 'antd/dist/antd.css';
import {
  SearchOutlined,
  StopOutlined,
  UserOutlined,
  LeftOutlined,
  DoubleRightOutlined,
} from "@ant-design/icons";
import './App.css';

const App = () => {
const { Option } = Select;

const [data, setData] = useState({});

  const [form] = Form.useForm();


const testData = async () => {
    try {
        const res = await axios.post('http://localhost:8000/api/user', data);
        console.log(res);
    } catch (error) {
        console.log(error);
    }

}

const handleChange = ({ target }) => {
  const { name, value } = target;
  const newData = Object.assign({}, data, { [name]: value });
  setData(newData);
}

const handleSubmit = (e) => {
  e.preventDefault();
  console.log(data);
  testData()
form.resetFields();
};

  const prefixSelector = (
    <Form.Item name="prefix" noStyle>
      <Select
        style={{
          width: 50,
          // height: 10,
        }}
      >
        <Option value="86">+86</Option>
        <Option value="87">+87</Option>
      </Select>
    </Form.Item>
  );
  return (
    <div>
    <div className="customizedCards">
  <Card className="cardStyle">
    <div className="main-form">
      <h5 className="idDetails">StudentDETAILS</h5>
      <Form style={{marginLeft: "-10px"}} form={form}>
      <Form.Item
    name="name"
    noStyle
    rules={[{ required: true, message: 'firstname is required' }]}
  >
    <Input type="text" name='first_name'  style={{ width: 400 }} onChange={handleChange}  placeholder="Firstname" />
  </Form.Item>
  <Form.Item
    name="name"
    noStyle
    rules={[{ required: true, message: 'lastname is required' }]}
  >
    <Input type="text" name='last_name' style={{ width: 400 }} onChange={handleChange} placeholder="Lastname" />
  </Form.Item>
  <Form.Item
    name="name"
    noStyle
    rules={[{ required: true, message: 'email is required' }]}
  >
    <Input type="email" name='email' style={{ width: 400 }} onChange={handleChange} placeholder="Email" />
  </Form.Item>
  <Form.Item
name="phone"
rules={[
  {
    required: true,
    message: 'Please input your phone number!',
  },
]}
>
<Input type="number" name='phone_number' addonBefore={prefixSelector} style={{ width: 400 }} onChange={handleChange} placeholder="Phone Number"  />
</Form.Item>
<Button  onClick={(e) => handleSubmit(e)} className="submit" type="primary" >Submit</Button>
      </Form>
    </div>
  </Card>
</div>
</div>
  )
}

export default App
````

Form component should wraps Form.Item Form组件应该包装Form.Item

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

https://ant.design/components/form https://ant.design/components/form

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 React 中单击提交按钮时如何重置表单 - How to Reset a form when I click submit button in React 使用纯 React.js Hooks 提交表单后如何重置我的输入字段,没有库 - How to reset my input field after I submit form using pure React.js Hooks, no libraries 当我使用 Hooks 在 React 中单击按钮时如何更改按钮背景颜色 - How to change button background color when I click the button in React using Hooks 当我使用反应钩子单击按钮时如何更改背景颜色 - how to change background color when I click the button using react hooks 单击提交按钮后如何使用反应重置表单? - How to reset a form after clicking submit button by using react? 单击按钮后反应挂钩 useInterval 重置 - React hooks useInterval reset after button click 单击表单提交按钮后,如何在反应 js 中关闭 model? - How can i close model in react js after i click form submit button? 使用 React 创建表单,如何让我的 err 消息在提交时显示(单击提交按钮时)? - Using React to create a form, how could I get my err message to display on Submit (when the submit button is clicked)? 在 reactjs 挂钩中提交后如何重置表单 - How can I reset form after submit in reactjs hooks 如何使用钩子提交表单以使其在表单上发布内容 - How to submit form in react using hooks so that it posts the things on the form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM