Home > Net >  how can I fetch random data by product id or slug in a single component in useQuery? its showing the
how can I fetch random data by product id or slug in a single component in useQuery? its showing the

Time:10-06

this is my code.

const fetchCartItem = async () => {
    if (token) {const {data } = await axios.get(API.GET_PRODUCT_DETAILS_BY_PRODUCT_ID.replace("[ProductID]",item?.ProductID),{headers:{Authorization: token,},});setCartLoading(false);if (data?.product === undefined) {return {};}else {return data?.product ? data?.product : {};}}};
  const{isLoading: itemLoading,refetch: cartRefetch,data: cart,} = useQuery(["cart"], () => fetchCartItem());

CodePudding user response:

If you want to fetch data according to your object ID in useQuery then you just need to pass the ID before calling the function. For ex:


const { isLoading: itemLoading, refetch: cartRefetch, data: cart} = useQuery(["cart", item?.ProductID], () => fetchCartItem());

  • Related