onMutate and TS
I'm trying to make an optimistic update on a query. but the
newQuote param on onMutate states that it is of type void? But printing it clearly reveals that it is of type TrackdayPlannerQuoteResponse.
return useMutation<TrackdayPlannerQuoteResponse>(
(data) =>
axios
.post(
`${process.env.GATSBY_API_URL}/trackday-planner/update-quote`,
data,
{
headers: {
"Content-Type": "application/json",
},
}
)
.then((response) => response.data),
{
onMutate: async (newQuote) => {
await queryClient.cancelQueries([...queryKey, newQuote.quoteId]);
const previousQuote = queryClient.getQueryData([
...queryKey,
newQuote.quoteId,
]);
queryClient.setQueryData([...queryKey, newQuote.quoteId], newQuote);
},
onError: (error, _, context) => {
console.error("update quote", error);
toast.error(error);
queryClient.setQueryData([...queryKey, context.quoteId], context);
},
onSettled: (data) => {
queryClient.cancelQueries([...queryKey, data.quoteId]);
},
}
);
return useMutation<TrackdayPlannerQuoteResponse>(
(data) =>
axios
.post(
`${process.env.GATSBY_API_URL}/trackday-planner/update-quote`,
data,
{
headers: {
"Content-Type": "application/json",
},
}
)
.then((response) => response.data),
{
onMutate: async (newQuote) => {
await queryClient.cancelQueries([...queryKey, newQuote.quoteId]);
const previousQuote = queryClient.getQueryData([
...queryKey,
newQuote.quoteId,
]);
queryClient.setQueryData([...queryKey, newQuote.quoteId], newQuote);
},
onError: (error, _, context) => {
console.error("update quote", error);
toast.error(error);
queryClient.setQueryData([...queryKey, context.quoteId], context);
},
onSettled: (data) => {
queryClient.cancelQueries([...queryKey, data.quoteId]);
},
}
);
0 Replies