Keep data while is loading using placeholderData
Hey, I was wondering if you could help me with something
I have a page with products and when I delete a product, I need to refetch a specific function to update the list without the deleted product. So I have something like:
queryKey: ['my-key', products]
I was using isPending to show an initial loading state, but every time I deleted a product, the loading state would appear. So, I did some research and learned about placeholderData and it worked. I added placeholderData: (prev) => prev, but when there's only one product in the cart and I delete it, the product still appears.
How can I fix this?3 Replies
correct-apricot•11mo ago
You can import keepPreviousData from the library
And pass that into the above
You can also check if the data is placeholder data or not
correct-apricotOP•11mo ago
how's that?
national-gold•11mo ago
useQuery returns a flag isPlaceHolderData in v5