在 React 中创建表单的最佳方法是什么?

编程入门 行业动态 更新时间:2024-10-24 06:34:38
本文介绍了在 React 中创建表单的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是 React 的初学者.我有以下代码:

I am beginner in react. I have following code:

import React, { useState, useEffect } from 'react'; import { Card, Form, Button } from 'react-bootstrap'; import Axios from 'axios' export function StudentForm({ student, onSuccess, onError, setState }) { const url = `localhost:9899/api/StudentData`; const intialStudent = { Firstname: '', Middlename: '', Lastname: '', DOB: '', Gender: '' }; const [Student, setStudent] = useState(intialStudent); useEffect(() => { setStudent(student ? student : intialStudent); }, [student]); const SaveData = function (studentData) { if (student._id) { Axios.post(url, { ...studentData }, { headers: { 'accept': 'application/json' } }) .then(res => { setState(null); onSuccess(res); }) .catch(error => { alert('Error To Edit data'); }); } else { Axios.post(url, studentData, { headers: { 'accept': 'application/json' } }) .then(res => { setState(null); onSuccess(res); }) .catch(err => onError(err)); } } return ( <Card> <Card.Header><h5>{student ? "Edit" : "Add"} Student</h5></Card.Header> <Card.Body> <Form onSubmit={(e) => { e.preventDefault(); SaveData(Student); }}> <Form.Group><Form.Control type="text" name="Firstname" placeholder="Firstname" value={Student.Firstname} onChange={e => { setStudent({ ...Student, Firstname: e.target.value }) }} /></Form.Group> <Form.Group><Form.Control type="text" name="Middlename" placeholder="Middlename" value={Student.Middlename} onChange={e => setStudent({ ...Student, Middlename: e.target.value })} /></Form.Group> <Form.Group><Form.Control type="text" name="Lastname" placeholder="Lastname" value={Student.Lastname} onChange={e => setStudent({ ...Student, Lastname: e.target.value })} /></Form.Group> <Form.Group><Form.Control type="date" name="DOB" placeholder="DOB" value={Student.DOB} onChange={e => setStudent({ ...Student, DOB: e.target.value })} /></Form.Group> <Form.Group><Form.Control type="text" name="Gender" placeholder="Class" value={Student.Gender} onChange={e => setStudent({ ...Student, Gender: e.target.value })} /></Form.Group> <Button variant="primary" type="submit">Submit</Button> </Form> </Card.Body> </Card> ); }

在上面的代码中,我在每个字段的更改事件上设置状态.因此,当我更改任何字段时,它会一次又一次地呈现.如果它是大形式,那么重新呈现可能需要很多时间,所以有没有更好的方法来创建来处理这种情况,或者任何最好的方法在 React 中使用表单的实践?

In above code I am setting state on change event on each field. So it will render again and again when I change any of the field.If it is large form so it may take a lot of time to re-render so is there a better way to create to handle this kind of situation, or any best practices for using forms with react?

推荐答案

对于所有 onChanges,您只能使用一个 Function.看起来像这样;

You can use only one Function for all onChanges. Looks like this;

<Form.Group> <Form.Control type="text" name="Firstname" placeholder="Firstname" value={Student.Firstname} onChange={handleChange} /> </Form.Group>

这是你的handleChange函数;

And this is your handleChange function;

const handleChange = e => { const {name, value} = e.target setValues({...values, [name]: value}) }

这是你的状态;

const [values, setValues] = useState({ Firstname: "", Middlename: "", Lastname: "", DOB: "", Gender: "" })

我认为这种方式用更少的代码更有效.

I think this way is more effective with less code.

更多推荐

在 React 中创建表单的最佳方法是什么?

本文发布于:2023-07-17 06:46:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1130525.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   方法   React

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!