I'm developing an online store using React. I have a functionality where, upon clicking a button, a product is added to favorites. After this action, I want to update the profile request (which returns user and favorites).
My code:
// useProfile.ts
import UserService from "@/services/user.service";
import { useQuery } from "@tanstack/react-query";
import { useUser } from "./useUser";
export const useProfile = () => {
const { user } = useUser();
const { data } = useQuery({
queryKey: ['profile'],
queryFn: () => {
return UserService.getProfile(user?.id || "");
},
select: ({ data }) => data
});
return {profile: data};
};
// LikeButton.tsx
import { useRef } from 'react';
import classes from './LikeButton.module.scss';
// @ts-ignore
import { ReactComponent as IconHeart } from '@/assets/img/svg/icon-heart.svg';
import { useProfile } from '@/hooks/useProfile';
import UserService from '@/services/user.service';
import { useMutation, useQueryClient } from '@tanstack/react-query';
interface ILikeButtonProps {
productId: number;
}
function LikeButton({ productId }: ILikeButtonProps) {
const { profile } = useProfile();
const { invalidateQueries } = useQueryClient();
const { mutate } = useMutation({
mutationKey: ['toggle favourite'],
mutationFn: () => UserService.toggleFavourite(productId),
onSuccess: () => {
console.log('onSuccess');
invalidateQueries({ queryKey: ['profile'] });
},
});
const btnRef = useRef<HTMLButtonElement>(null);
const buttonClasses = profile?.favourites.some((fp) => fp.id === productId)
? `${classes.btn_like} ${classes['btn_like--active']}`
: classes.btn_like;
return (
<button
ref={btnRef}
className={buttonClasses}
onClick={() => mutate()}
data-product-id={productId}
>
<IconHeart />
</button>
);
}
export default LikeButton;
However, for some reason, invalidateQueries doesn't seem to work, or perhaps I'm doing something incorrectly... I couldn't find an answer online :(
I added console.log - it works perfectly. But invalidateQueries isn't being triggered.