简体   繁体   English

在 React 中单击提交按钮时如何重置表单

[英]How to Reset a form when I click submit button in React

I am working on a React project, For designing I am using Ant design framework.我正在做一个 React 项目,为了设计,我使用了 Ant 设计框架。 I have a form in that form when I entered all details in Input form and when I click submit button the Form has to be Reset, I tried to Reset a form after clicking submit button.当我在输入表单中输入所有详细信息时,我在该表单中有一个表单,当我单击提交按钮时,表单必须重置,我尝试在单击提交按钮后重置表单。 but it is showing this kind of error.但它显示了这种错误。 TypeError: Cannot read property 'reset' of null类型错误:无法读取 null 的属性“重置”

So please help me to resolve this error所以请帮我解决这个错误

This is my code App.js这是我的代码 App.js

import React, { useRef, 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 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()
};
const myForm = useRef(null)
    const resetForm = () => {
        myForm.current.reset();
        }
  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"}} ref={myForm}>
      <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), resetForm()} className="submit" type="primary" >Submit</Button>
      </Form>
    </div>
  </Card>
</div>
</div>
  )
}

export default App
```

As said here you just have a syntax trouble.正如这里所说你只是有一个语法问题。

Try this instead:试试这个:

const myForm = useRef(null)
const resetForm = () => {
    myForm.reset();
}

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

相关问题 如何重置表单当我单击提交按钮时,我使用 React 钩子做出反应 - How to Reset a form When I click submit Button I React using React hooks 单击提交按钮后如何使用反应重置表单? - How to reset a form after clicking submit button by using react? 单击表单提交按钮后,如何在反应 js 中关闭 model? - How can i close model in react js after i click form submit button? 如何在提交表单后重置表单并启用提交按钮(react-formio)? - how to reset the form and enable submit button after form submision (react-formio)? 当我在反应中单击提交按钮时,如何显示加载指示器 5 秒? - How to show loading indicator for 5 seconds when I click the submit button in react? 如何通过按下按钮重置 React 组件中的表单值? - How can I reset the form values in a React component with a button press? 点击提交按钮时,React 表单不调用提交功能 - React form not calling submit function upon submit button click 点击提交按钮,执行 function 然后提交表单 react.js - on submit button click, execute function then submit form react.js 当 onBlur 事件上的按钮消失时,如何使表单提交工作? 反应,下一个js - How do I make a form submit work when button disappears on onBlur event? React, next js 单击提交按钮时反应显示错误 - react shows error when submit button click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM