You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//Decrease product quantityconst{mutate: decreaseProductQuantity,isPending: isPendingDecreaseProductQuantity}=useMutation({mutationFn: async()=>{constupdated_cart_quantity: number|undefined=queryClient.getQueryData(["cart_quantity"])constupdated_cart_products: ICartProduct[]|undefined=queryClient.getQueryData(["cart_products"])/* update cart_products and cart_quantity in DB */const{error: cart_quantity_error}=awaitsupabaseClient.from("users_cart").update({cart_quantity: updated_cart_quantity}).eq("id",userStore.userId)if(cart_quantity_error)throwcart_quantity_errorconst{error: cart_products_error}=awaitsupabaseClient.from("users_cart").update({cart_products: updated_cart_products}).eq("id",userStore.userId)if(cart_products_error)throwcart_products_error},onMutate: ()=>{/* logic to update cart_quantity optimistically *///update cart_quantity firstconstcart_quantity: number|undefined=queryClient.getQueryData(["cart_quantity"])letupdated_cart_quantity=cart_quantityif(cart_quantity===0){returncart_quantity}elseif(updated_cart_quantity!==undefined){updated_cart_quantity-=1}/* logic to update cart_products optimistically */constcart_products: ICartProduct[]|undefined=queryClient.getQueryData(["cart_products"])letupdated_cart_products=cart_productsif(productQuantity===0){returnproductQuantity}elseif(updated_cart_products!==undefined){updated_cart_products[updated_cart_products?.findIndex(productInCart=>productInCart.id===product.id)].quantity-=1//leave products in cart only if product.quantity > 0updated_cart_products=updated_cart_products.filter(productInCart=>productInCart.quantity>0)setProductQuantity(productQuantity-1)}queryClient.setQueryData(["cart_quantity"],updated_cart_quantity)queryClient.setQueryData(["cart_products"],updated_cart_products)},onError: ()=>{/* logic to rollback cart_quantity *///update cart_quantity firstconstcart_quantity: number|undefined=queryClient.getQueryData(["cart_quantity"])letupdated_cart_quantity=cart_quantityif(updated_cart_quantity===0){returncart_quantity}elseif(updated_cart_quantity!==undefined){updated_cart_quantity+=1}/* logic to rollback cart_products */constcart_products: ICartProduct[]|undefined=queryClient.getQueryData(["cart_products"])letupdated_cart_products=cart_productsif(productQuantity===0){returnproductQuantity}elseif(updated_cart_products!==undefined&&productQuantity+1===1){//no way to rollback this because I setProductQuantity(1-1)}elseif(updated_cart_products!==undefined){updated_cart_products[updated_cart_products.findIndex(productInCart=>(productInCart.id=product.id))].quantity+=1setProductQuantity(productQuantity+1)}queryClient.setQueryData(["cart_quantity"],updated_cart_quantity)queryClient.setQueryData(["cart_products"],updated_cart_products)},})
So I do this logic to rollback on error - with context it would be easer
Describe the bug
I got context undefined when I tried to use it
So I do this logic to rollback on error - with context it would be easer
Your minimal, reproducible example
example
Steps to reproduce
Here it works (I can get access to context)
So to reproduce it you may git clone repo where I write this code
https://github.com/nicitaacom/23_store/blob/development/app/(site)/components/Product.tsx
And console.log(context)
and you got undefined (also you need .env so set up supabase if you want or just trust me)
Expected behavior
I want to use context
But I got context undefined
How often does this bug happen?
Sometimes
Screenshots or Videos
No response
Platform
v5
Tanstack Query adapter
react-query
TanStack Query version
v5
TypeScript version
latest
Additional context
You may move it to discussions if you want to
I think its bug because I do similar things
The text was updated successfully, but these errors were encountered: