how can I fetch random data by product id or slug in a single component in useQuery? its showing the same data for all component

Issue

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());

Solution

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());

Answered By – Mahfuz Rahman

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published