在打字稿和ReactQuery中,如何将道具传递给外部异步组件?

发布于 2025-02-11 07:18:07 字数 1098 浏览 2 评论 0原文

我在下一步 我的行

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情空心 2025-02-18 07:18:07

您可以进一步解释问题是什么吗?目前尚不清楚出现了什么问题以及您期望发生什么

编辑:
我看到了问题。实际上,您正在调用getProducts useQuery。您应该将功能传递给usequery
如果您想能够参数化函数,那么您应该创建所谓的关闭内容,例如:

const getProducts = (id: number) => {
    return async () => {
        const rest = await fetch(`https://fakestoreapi.com/products/${id}`);
        const data: ProductApi[] = await rest.json();
        return data;
    }
}

这里发生的事情是,我返回一个异步函数,当称为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 to useQuery. You should pass a function to useQuery.
If you want to be able to parameterise the function, then you should create what's called a closure, like so:

const getProducts = (id: number) => {
    return async () => {
        const rest = await fetch(`https://fakestoreapi.com/products/${id}`);
        const data: ProductApi[] = await rest.json();
        return data;
    }
}

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

最好是你 2025-02-18 07:18:07
useQuery("products", getProducts(1))

此代码将在渲染过程中调用函数getProducts(1),而不是传递函数来Quart-Query-实际上,您将函数的结果传递给了React-Query。
大多数示例使用内联函数

useQuery("products", () => getProducts(1))

此外,ID理想情况下应该是Querykey的一部分,因为这是对此的依赖性:

const id = 1

useQuery(["products", id], () => getProducts(id))

如果您这样做,您还可以利用React-Query将Query键注入您传递的函数的事实它:

useQuery(["products", 1], ({ queryKey }) => getProducts(queryKey[1]))

最好的做法是唯一的具有您的查询功能取决于QueryKey内部的内容。

useQuery("products", getProducts(1))

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:

useQuery("products", () => getProducts(1))

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:

const id = 1

useQuery(["products", id], () => getProducts(id))

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:

useQuery(["products", 1], ({ queryKey }) => getProducts(queryKey[1]))

It is best practice to only have your query function depend on things that are also inside the queryKey.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文