gpt4 book ai didi

javascript - 在 React 中创建表单的最佳方式是什么?

转载 作者:行者123 更新时间:2023-11-29 20:27:25 25 4
gpt4 key购买 nike

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

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 = `http://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 中使用表单的实践?

最佳答案

您只能对所有 onChanges 使用一个函数。看起来像这样;

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

这是您的 handleChange 函数;

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

这是你的状态;

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

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

关于javascript - 在 React 中创建表单的最佳方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58969217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com