【发布时间】:2021-07-02 19:54:43
【问题描述】:
我的 Google 图书克隆应用程序让用户提交他们输入的查询,然后显示响应数据。
按照 React Hook Form 文档提供的schema validation example,我收到了Error: Request failed with status code 400。我做错了什么?
const [query, setQuery] = useState("");
const [books, setBooks] = useState({ items: [] });
const {
register,
handleSubmit,
formState: { errors }
} = useForm({
resolver: yupResolver(schema)
});
const handleBookFetch = async () => {
try {
const result = await axios.get(`${API_BASE_URL}?q=${query}`);
setBooks(result.data);
} catch (error) {
console.log(error);
}
};
<form onSubmit={handleSubmit(handleBookFetch)}>
<label name="book">
Search the world's most comprehensive index of full-text books.
</label>
<div>
<input type="text" name="book" {...register("book")} onChange={(event) => setQuery(event.target.value)} />
<p>{errors.book?.message}</p>
<button type="submit">Search</button>
</div>
</form>
【问题讨论】:
标签: reactjs react-hooks react-hook-form