在React Query v4中是否有一种方法可以根据查询数据的类型进行转换和映射?

huangapple go评论80阅读模式
英文:

Is there any way to transform query data and map according to that type in react-query v4?

问题

I am trying to transform query data from API response. More specifically I want to add two additional properties. That is why I use select configuration in useQuery hook like this:
在React Query v4中是否有一种方法可以根据查询数据的类型进行转换和映射?
But in this case, I didn't get a suggestion for the extra two properties. That's why I've added another model and used it with the useQuery hook like this:
在React Query v4中是否有一种方法可以根据查询数据的类型进行转换和映射?
But it's throwing an error.

I am stuck with this.

Edit on codesandbox

英文:

I am trying to transform query data from API response. More specifically I want to add two additional property. That is why I use select configuration in useQuery hook like this:
在React Query v4中是否有一种方法可以根据查询数据的类型进行转换和映射?
But in this case I didn't get suggestion for extra two properties. That's why I've add another model and use it with useQuery hook like this:
在React Query v4中是否有一种方法可以根据查询数据的类型进行转换和映射?
But it troughing error.

I am stucked with this.

edit on codesandbox

答案1

得分: 3

The problem is that the first generic type parameter of useQuery should not be the returned data type but the queryFn return type. You can put ModifiedProduct[] as the third generic type parameter or let the code infer it.

  const { data, isLoading } = useQuery({
    queryKey: ["fetch-products"],
    queryFn: fetchProducts,
    select: (data) => {
      const items = data.map(
        (prod): ModifiedProduct => ({
          ...prod,
          dateOfAdd: new Date(),
          dateOfUpdate: new Date()
        })
      );
      return items;
    }
  });

// This should also work
  const { data, isLoading } = useQuery<Product[], unknown, ModifiedProduct[]>({
    queryKey: ["fetch-products"],
    queryFn: fetchProducts,
    select: (data) => {
      const items = data.map(
        (prod): ModifiedProduct => ({
          ...prod,
          dateOfAdd: new Date(),
          dateOfUpdate: new Date()
        })
      );
      return items;
    }
  });
英文:

Try this, the probblem is that the first generic type parameter of useQuery is not the returned data type. It's the queryFn return type. You can put the ModifiedProduct[] to the third generic type parameter or that the code infer itself.

  const { data, isLoading } = useQuery({
    queryKey: [&quot;fetch-products&quot;],
    queryFn: fetchProducts,
    select: (data) =&gt; {
      const items = data.map(
        (prod): ModifiedProduct =&gt; ({
          ...prod,
          dateOfAdd: new Date(),
          dateOfUpdate: new Date()
        })
      );
      return items;
    }
  });

// this should also work
  const { data, isLoading } = useQuery&lt;Product[], unknown, ModifiedProduct[]&gt;({
    queryKey: [&quot;fetch-products&quot;],
    queryFn: fetchProducts,
    select: (data) =&gt; {
      const items = data.map(
        (prod): ModifiedProduct =&gt; ({
          ...prod,
          dateOfAdd: new Date(),
          dateOfUpdate: new Date()
        })
      );
      return items;
    }
  });



huangapple
  • 本文由 发表于 2023年5月15日 13:27:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76251091.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定