T
TanStack3y ago
ambitious-aqua

useQuery does not emit data to parent when useMutation happens in child

export function setNewCart() {
const {setStorageItem} = useAsyncStorage();
const queryClient = useQueryClient();

const {mutateAsync} = useMutation(
async (basket: Basket) => {
await setStorageItem(JSON.stringify(basket), BASKET_VARIABLE);
return basket;
},
{
onSuccess: (newBasket) => {
console.log("====================================");
console.log(newBasket.totalItems);
console.log("====================================");
queryClient.setQueryData([BASKET_VARIABLE], newBasket);
},
},
);

return {mutateAsync};
}
export function setNewCart() {
const {setStorageItem} = useAsyncStorage();
const queryClient = useQueryClient();

const {mutateAsync} = useMutation(
async (basket: Basket) => {
await setStorageItem(JSON.stringify(basket), BASKET_VARIABLE);
return basket;
},
{
onSuccess: (newBasket) => {
console.log("====================================");
console.log(newBasket.totalItems);
console.log("====================================");
queryClient.setQueryData([BASKET_VARIABLE], newBasket);
},
},
);

return {mutateAsync};
}
So I have this code to store a new shopping basket and then update the cached basket so any useQuery with the same key can get the updates
export function useCart() {
const {data} = useQuery<Basket>([BASKET_VARIABLE], getBasketFromStorage);

return {basket: data || getInitialBasketState()};
}
export function useCart() {
const {data} = useQuery<Basket>([BASKET_VARIABLE], getBasketFromStorage);

return {basket: data || getInitialBasketState()};
}
This is the query to get the updated basket data, problem is that it is not sending out updates even though I get the new basket from OnSuccess
1 Reply
foreign-sapphire
foreign-sapphire3y ago
probably the keys aren't matching. What do the devtools say?

Did you find this page helpful?