gpt4 book ai didi

reactjs - React-hook-form的 'reset'工作正常,提交后输入字段仍不为空

转载 作者:行者123 更新时间:2023-12-03 15:27:22 45 4
gpt4 key购买 nike

我尝试使用“重置”功能形式react-hook-form,但是在提交输入字段后不清空。
我不知道为什么会这样,我确定我缺少了一些东西却找不到。
这是我的代码:

const Form = () => {
const [values, setValues] = useState({
email: "",
name: "",
subject: "",
description: "",
});

const { register, handleSubmit, reset, errors } = useForm();


toastr.options = {"positionClass": "toast-top-right","progressBar": true,}
const onSubmit = (values, e) => {

const { email, name, subject, description } = values;
axios.post("http://localhost:8080/sendme", {
email,
name,
subject,
text: description,
});

e.target.reset();
toastr.success('Message was sent successfully!');

};

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

};


return (
<div>
<form onSubmit={handleSubmit(onSubmit)} noValidate>
<div className="inputField">
<input
className={`${errors.email && "inputError"}`}
name="email"
type="email"
ref={register({ required: true, pattern: /^\S+@\S+$/i })}
placeholder="Your email *"
value={values.email}
onChange={handleChange}
/>
<ErrorMessage error={errors.email} />
</div>
<div className="inputField">
<input
className={`${errors.name && "inputError"}`}
name="name"
type="text"
placeholder="Your name *"
ref={register({ required: true })}
value={values.name}
onChange={handleChange}
/>
<ErrorMessage error={errors.name} />
</div>
<div className="inputField">
<input
className={`${errors.subject && "inputError"}`}
name="subject"
type="text"
placeholder="Subject *"
ref={register({ required: true })}
value={values.subject}
onChange={handleChange}
/>
<ErrorMessage error={errors.subject} />
</div>
<div className="inputField">
<p className="reqTxt"> * = Required</p>
<textarea
className={`${errors.description && "inputError"}`}
name="description"
placeholder="Type your message here *"
ref={register({ required: true, minLength: 15 })}
value={values.description}
onChange={handleChange}
rows="15"
cols="80"
></textarea>
<ErrorMessage error={errors.description} />
</div>

<button className="btn" onClick={reset} type="submit">
Send message
</button>
</form>
</div>
);
};
我已经导入了重置并将其与onClick一起使用,但是它似乎不起作用。
我该如何解决?

最佳答案

使用react-hook-form时,很可能可以使用useState跳过:
https://react-hook-form.com/get-started
这是入门页面上的一个简单示例:

import React from "react";
import { useForm } from "react-hook-form";

export default function App() {
const { register, handleSubmit, watch, errors } = useForm();
const onSubmit = data => console.log(data);

console.log(watch("example")); // watch input value by passing the name of it

return (
{/* "handleSubmit" will validate your inputs before invoking "onSubmit" */}
<form onSubmit={handleSubmit(onSubmit)}>
{/* register your input into the hook by invoking the "register" function */}
<input name="example" defaultValue="test" ref={register} />

{/* include validation with required or other standard HTML validation rules */}
<input name="exampleRequired" ref={register({ required: true })} />
{/* errors will return when field validation fails */}
{errors.exampleRequired && <span>This field is required</span>}

<input type="submit" />
</form>
);
}

关于reactjs - React-hook-form的 'reset'工作正常,提交后输入字段仍不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63058021/

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