为什么反应挂钩表单错误对象暂时为空?
Why does react hook form errors object temporarily empty?
我试图理解为什么反应挂钩表单中的错误对象变为空,即使在为它设置了错误之后也是如此。我在代码沙箱中构建了一个演示,这应该有助于更清楚地解释这一点。
所以我有一个在更改字段后运行的函数:
const onNameChange = async ({ target: { value } }) => {
setValue("name", value);
const valid = await trigger("name");
console.log("valid", valid, "value", value);
if (!valid) {
return;
}
getPokemonDebounced(value);
setShowPokemon(false);
};
它运行 getPokemonDebounced
函数:
const getPokemon = async (input) => {
console.log("getPokemon Fn");
try {
const res = await axios.get(`https://pokeapi.co/api/v2/pokemon/${input}`);
console.log(res);
const { data } = res;
if (data) {
setPokemon(data);
setFetchError(null);
}
console.log(res);
} catch (error) {
setPokemon(null);
setFetchError(error);
setError("name", { type: "manual", message: "CUSTOM name error" });
}
};
// why does the errors object temporarily become blank? nothing seems to set it to blank
const getPokemonDebounced = useDebounce(500, getPokemon);
当 API 调用失败时设置错误。但是,我注意到在这种情况下错误对象变为空:
- 访问演示:
https://codesandbox.io/s/react-playground-forked-tv0cm?file=/Pokemon.js
- 打开控制台(在显示屏的左下方)
- 在字段中输入一些乱码,例如 'wefhiwd'
- 等待错误对象记录(应该有一个名称键)
- 在该字段中再输入 1 个字母,您会看到错误对象已被记录并且暂时是一个空对象
{}
,直到 getPokemonDebounced 运行并且无法获取以设置错误
我的问题是为什么错误对象在输入另一个字母后立即再次变为空 {}
?输入最初的乱码后,它将错误对象设置为包含错误,我暂时看不出它是如何再次变空的。
表格:
<form onSubmit={handleSubmit(onSubmit /*, onError*/)}>
<input
{...register("name", { required: true })}
name="name"
placeholder="Enter a pokemon"
onChange={onNameChange}
/>
<button type="submit" onClick={onSubmit}>
Show Pokemon
</button>
{errors.name && <p>{errors.name.message}</p>}
</form>
感谢您的帮助
因为在更改您的输入时,您会再次 运行 验证并清除所有有效字段上的错误。
const onNameChange = async ({ target: { value } }) => {
setValue("name", value);
const valid = await trigger("name"); // here you trigger validation that will clear the error if the field is valid
console.log("valid", valid, "value", value);
if (!valid) { // if invalid nothing happens
return;
}
getPokemonDebounced(value); // if valid do the call which will set the new error on fail
setShowPokemon(false);
};
如果您使用与输入无关的名称设置错误,它将持续存在。
setError("randomError", { type: "manual", message: "RANDOM name error" });
来自文档 - https://react-hook-form.com/api/useform/seterror/
- This method will not persist the associated input error if the input passes validation.
- An error that is not associated with an input field will be persisted until cleared with clearErrors.
我试图理解为什么反应挂钩表单中的错误对象变为空,即使在为它设置了错误之后也是如此。我在代码沙箱中构建了一个演示,这应该有助于更清楚地解释这一点。
所以我有一个在更改字段后运行的函数:
const onNameChange = async ({ target: { value } }) => {
setValue("name", value);
const valid = await trigger("name");
console.log("valid", valid, "value", value);
if (!valid) {
return;
}
getPokemonDebounced(value);
setShowPokemon(false);
};
它运行 getPokemonDebounced
函数:
const getPokemon = async (input) => {
console.log("getPokemon Fn");
try {
const res = await axios.get(`https://pokeapi.co/api/v2/pokemon/${input}`);
console.log(res);
const { data } = res;
if (data) {
setPokemon(data);
setFetchError(null);
}
console.log(res);
} catch (error) {
setPokemon(null);
setFetchError(error);
setError("name", { type: "manual", message: "CUSTOM name error" });
}
};
// why does the errors object temporarily become blank? nothing seems to set it to blank
const getPokemonDebounced = useDebounce(500, getPokemon);
当 API 调用失败时设置错误。但是,我注意到在这种情况下错误对象变为空:
- 访问演示: https://codesandbox.io/s/react-playground-forked-tv0cm?file=/Pokemon.js
- 打开控制台(在显示屏的左下方)
- 在字段中输入一些乱码,例如 'wefhiwd'
- 等待错误对象记录(应该有一个名称键)
- 在该字段中再输入 1 个字母,您会看到错误对象已被记录并且暂时是一个空对象
{}
,直到 getPokemonDebounced 运行并且无法获取以设置错误
我的问题是为什么错误对象在输入另一个字母后立即再次变为空 {}
?输入最初的乱码后,它将错误对象设置为包含错误,我暂时看不出它是如何再次变空的。
表格:
<form onSubmit={handleSubmit(onSubmit /*, onError*/)}>
<input
{...register("name", { required: true })}
name="name"
placeholder="Enter a pokemon"
onChange={onNameChange}
/>
<button type="submit" onClick={onSubmit}>
Show Pokemon
</button>
{errors.name && <p>{errors.name.message}</p>}
</form>
感谢您的帮助
因为在更改您的输入时,您会再次 运行 验证并清除所有有效字段上的错误。
const onNameChange = async ({ target: { value } }) => {
setValue("name", value);
const valid = await trigger("name"); // here you trigger validation that will clear the error if the field is valid
console.log("valid", valid, "value", value);
if (!valid) { // if invalid nothing happens
return;
}
getPokemonDebounced(value); // if valid do the call which will set the new error on fail
setShowPokemon(false);
};
如果您使用与输入无关的名称设置错误,它将持续存在。
setError("randomError", { type: "manual", message: "RANDOM name error" });
来自文档 - https://react-hook-form.com/api/useform/seterror/
- This method will not persist the associated input error if the input passes validation.
- An error that is not associated with an input field will be persisted until cleared with clearErrors.