您可以参考此处了解功能组件中受控输入的用法。
您正在错误地初始化状态,
const [submit, SetSubmit] = useState('');
此外, onChange 事件应该被处理为
<div>
<input type="text"
name = 'brand'
onChange = {inputChange}
value = {submit}
required />
</div>
并作为,
const inputChange = (e) => {
setSubmit(e.target.value)
}
如果是多个输入,请参考以下示例:
import React, { useState } from "react";
const initialValues = {
company: "",
position: "",
link: "",
date: "",
note: "",
};
export default function Form() {
const [values, setValues] = useState(initialValues);
const handleInputChange = (e) => {
const { name, value } = e.target;
setValues({
...values,
[name]: value,
});
};
return (
<form>
<input
value={values.company}
onChange={handleInputChange}
name="company"
label="Company"
/>
<input
value={values.position}
onChange={handleInputChange}
name="position"
label="Job Title"
/>
// ... Rest of the input fields
<button type="submit"> Submit </button>
</form>
);
}