I am new to React Query, I've read the documents and trying to implement it in my project.
My problem is this: when I mutate a data in my queries which I'm mapping and rendering in the component, the mutation happens but the list (my query) doesn't update unless with a refresh.
I can't find out what am I doing wrong.
here is my code,
My query:
const queryClient = useQueryClient();
const { isFetching, data, error, isLoading } = useQuery('dataList', async () => {
const res = await api.get_vouchers({ orderby: 'created_at desc', top: '50' });
return res.data;
});
My mutation: (The currentVoucher state is local and get set whenever an item in the list get clicked)
const patchVoucher = async () => {
await api.patch_voucher_enabled_code({ voucherCode: currentVoucher.code });
};
const voucherMutation = useMutation(patchVoucher, {
onSuccess: () => {
return queryClient.invalidateQueries('dataList');
},
});
and finally on my button's onClick where I fire mutation:
onClick={() => {
voucherMutation.mutate();
}}
I even tried the optimistic update, in that scenario my optimistic mutation worked well and the list was updated but when the code arrived to "queryClient.invalidateQueries('dataList')", the data changed to before mutation version. It caused me to see the mutated list for a second and then gone.