英文:
useQuery Hook result set to loading without being called
问题
这是您的翻译:
我正在尝试在我的新项目中构建一个搜索栏,似乎有一些(也许很多)错误。
我将书籍状态设置为null,useQuery钩子似乎正在使用它来搜索书籍。
除非我点击按钮,否则我不希望它搜索任何东西。
以下是我的代码:
fetchBooks.jsx
```jsx
async function fetchBooks({ queryKey }) {
const book = queryKey[1];
const response = await fetch(
`https://www.googleapis.com/books/v1/volumes?q=${book}`
);
if (!response.ok) {
throw new Error(`未找到关于${book}的搜索结果`);
}
return response.json();
}
export default fetchBooks;
这是主要组件。
import { useState } from "react";
import { useQuery } from "@tanstack/react-query";
import fetchBooks from "../helper/fetchBooks";
const Home = () => {
const [book, setBook] = useState(null);
const results = useQuery(["search", book], fetchBooks);
const handleSubmit = (e) => {
e.preventDefault();
setBook(e.target.elements.book.value);
};
return (
<>
<form onSubmit={handleSubmit}>
<label htmlFor="book">
书名:
<input type="text" name="book" />
</label>
<button type="submit">提交</button>
</form>
{results.isLoading ? (
<div>加载中...</div>
) : results.isError ? (
<div>{results.error.message}</div>
) : (
<div>
<h2>搜索结果</h2>
<ul>
{results.data.items.map((item) => {
return (
<div key={item.id}>
<h3>{item.volumeInfo.title}</h3>
<p>{item.volumeInfo.authors}</p>
</div>
);
})}
</ul>
</div>
)}
</>
);
};
export default Home;
英文:
I'm trying to build a search bar in my new project, and I seem to be doing some things(maybe a lot) wrong.
I set the book state to null and the useQuery hook seems to be using it to search for books.
I don't want it to search for anything unless I click the button.
These are my codes:
fetchBooks.jsx
async function fetchBooks({ queryKey }) {
const book = queryKey[1];
const response = await fetch(
`https://www.googleapis.com/books/v1/volumes?q=${book}`
);
if (!response.ok) {
throw new Error(`Search not found for ${book}`);
}
return response.json();
}
export default fetchBooks;
Here is the main component.
import { useState } from "react";
import { useQuery } from "@tanstack/react-query";
import fetchBooks from "../helper/fetchBooks";
const Home = () => {
const [book, setBook] = useState(null);
const results = useQuery(["search", book], fetchBooks);
const handleSubmit = (e) => {
e.preventDefault();
setBook(e.target.elements.book.value);
};
return (
<>
<form onSubmit={handleSubmit}>
<label htmlFor="book">
Book Name:
<input type="text" name="book" />
</label>
<button type="submit">Submit</button>
</form>
{results.isLoading ? (
<div>Loading...</div>
) : results.isError ? (
<div>{results.error.message}</div>
) : (
<div>
<h2>Results</h2>
<ul>
{results.data.items.map((item) => {
return (
<div key={item.id}>
<h3>{item.volumeInfo.title}</h3>
<p>{item.volumeInfo.authors}</p>
</div>
);
})}
</ul>
</div>
)}
</>
);
};
export default Home;
答案1
得分: 1
你可以在 fetch 函数中如果 book 为 null,则返回一个默认值。这样,查询实际上不会请求 API。
async function fetchBooks({ queryKey }) {
const book = queryKey[1];
if (!book) return { items: [] }
const response = await fetch(
`https://www.googleapis.com/books/v1/volumes?q=${book}`
);
if (!response.ok) {
throw new Error(`Search not found for ${book}`);
}
return response.json();
}
export default fetchBooks;
英文:
You can return a default value in the fetch function if the book is null. Then, the query won't actually request the API.
async function fetchBooks({ queryKey }) {
const book = queryKey[1];
if(!book) return { items: [] }
const response = await fetch(
`https://www.googleapis.com/books/v1/volumes?q=${book}`
);
if (!response.ok) {
throw new Error(`Search not found for ${book}`);
}
return response.json();
}
export default fetchBooks;
答案2
得分: 0
以下是翻译好的部分:
"Instead of restricting the useQuery
to call the fetchBooks
functions, you can modify the fetchBooks
functions to return an empty array if book
is set to null. The fetchBooks
can be modified as below:"
"不必将useQuery
限制为调用fetchBooks
函数,可以修改fetchBooks
函数以在book
设置为null时返回一个空数组。可以按照以下方式修改fetchBooks
:"
async function fetchBooks({ queryKey }) {
const book = queryKey[1];
if (!book) {
return {
isLoading: false,
error: null,
data: null
}
}
const response = await fetch(
`https://www.googleapis.com/books/v1/volumes?q=${book}`
);
if (!response.ok) {
throw new Error(`Search not found for ${book}`);
}
return response.json();
}
export default fetchBooks;
英文:
Instead of restricting the useQuery
to call the fecthBooks
functions, you can modify the fetchBooks
functions to return an empty array if book
is set to null. The fetchBooks
can be modified as below:-
async function fetchBooks({ queryKey }) {
const book = queryKey[1];
if(!book){
return {
isLoading : false,
error : null,
data : null
}
}
const response = await fetch(
`https://www.googleapis.com/books/v1/volumes?q=${book}`
);
if (!response.ok) {
throw new Error(`Search not found for ${book}`);
}
return response.json();
}
export default fetchBooks;
答案3
得分: 0
使用惯用方式是在参数未准备好时将 useQuery
本身设置为 disabled
(通过 enabled
属性):
const results = useQuery({
queryKey: ["search", book],
queryFn: fetchBooks,
enabled: !!books
})
这将在没有书籍时阻止查询函数的执行。
英文:
The idiomatic way would be to set the useQuery
itself to disabled
(via the enabled
property) when your params aren't ready:
const results = useQuery({
queryKey: ["search", book],
queryFn: fetchBooks
enabled: !!books
})
this will prevent the query function from executing when you have no books.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论