T
TanStack2y ago
other-emerald

How to use `useMutation` with fetch (not axios)

const postData = () => {
const res = await fetch("/profile", {
method: "POST",
header: {
// ...
}
body: {
// ...
}
);

const data = await res.json();
return data;
}


useMutation({
mutationFn: postData
})
const postData = () => {
const res = await fetch("/profile", {
method: "POST",
header: {
// ...
}
body: {
// ...
}
);

const data = await res.json();
return data;
}


useMutation({
mutationFn: postData
})
Is it correct?
3 Replies
complex-teal
complex-teal2y ago
Yes but api errors like 400 or 500 will not be caught
other-emerald
other-emeraldOP2y ago
ah~ thankyou If I add this code, it works. right?
if(!res.ok) throw Error("~~");
if(!res.ok) throw Error("~~");
complex-teal
complex-teal2y ago
Looks good to me

Did you find this page helpful?