在打字稿和ReactQuery中,如何将道具传递给外部异步组件?
我在下一步 我的行
const { isLoading, data, error } = useQuery("products", getProducts(1));
GetProducts(1)) - >我应该如何将道具传递给该组件?
页面:
import { useQuery } from "react-query";
import { getProducts } from "../components/task-1/getProducts-0";
export const ProductsPage = () => {
const { isLoading, data, error } = useQuery("products", getProducts(1));
if (isLoading) {
return <div>Loading...</div>;
}
if (!data || error) {
return <div>Error#!</div>;
}
return <>...</>;
};
export default ProductsPage;
组件:
export const getProducts = async (id: number) => {
const rest = await fetch(
`https://fakestoreapi.com/products/${id}`
);
const data: ProductApi[] = await rest.json();
return data;
};
interface ProductApi {
id: number;
title: string;
price: number;
description: string;
category: string;
image: string;
rating: Rating;
}
interface Rating {
rate: number;
count: number;
}
I have problem with pass props to external components in next.js
I have problem with line
const { isLoading, data, error } = useQuery("products", getProducts(1));
getProducts(1)) -> How I should pass props to this component?
page:
import { useQuery } from "react-query";
import { getProducts } from "../components/task-1/getProducts-0";
export const ProductsPage = () => {
const { isLoading, data, error } = useQuery("products", getProducts(1));
if (isLoading) {
return <div>Loading...</div>;
}
if (!data || error) {
return <div>Error#!</div>;
}
return <>...</>;
};
export default ProductsPage;
component:
export const getProducts = async (id: number) => {
const rest = await fetch(
`https://fakestoreapi.com/products/${id}`
);
const data: ProductApi[] = await rest.json();
return data;
};
interface ProductApi {
id: number;
title: string;
price: number;
description: string;
category: string;
image: string;
rating: Rating;
}
interface Rating {
rate: number;
count: number;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以进一步解释问题是什么吗?目前尚不清楚出现了什么问题以及您期望发生什么
编辑:
我看到了问题。实际上,您正在调用
getProducts
useQuery
。您应该将功能传递给usequery
。如果您想能够参数化函数,那么您应该创建所谓的关闭内容,例如:
这里发生的事情是,我返回一个异步函数,当称为API时,该功能是函数,这就是函数我想传递到
usequery
Could you perhaps further explain what the problem is? It's not really clear what's going wrong and what you expect to happen
Edit:
I see the problem. You are actually invoking the
getProducts
function in your call touseQuery
. You should pass a function touseQuery
.If you want to be able to parameterise the function, then you should create what's called a closure, like so:
What's happening here is, I'm return an async function that when called fetches the data from the API, and that is the function that I want to pass to
useQuery
此代码将在渲染过程中调用函数
getProducts(1)
,而不是传递函数来Quart-Query-实际上,您将函数的结果传递给了React-Query。大多数示例使用内联函数
。
此外,ID理想情况下应该是Querykey的一部分,因为这是对此的依赖性:
如果您这样做,您还可以利用React-Query将Query键注入您传递的函数的事实它:
最好的做法是唯一的具有您的查询功能取决于QueryKey内部的内容。
this code will invoke the function
getProducts(1)
during render rather than passing a function to react-query - you're actually passing the result of the function to react-query.Most example use inline functions for that matter:
now react-query gets a function that it can invoke.
Additionally, the id should ideally be part of the queryKey, because it's a dependency to it:
if you do it that way, you can also potentially take advantage of the fact that react-query will inject the query key into the function you pass to it:
It is best practice to only have your query function depend on things that are also inside the queryKey.