T
TanStack2y ago
correct-apricot

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
conscious-sapphire
conscious-sapphire2y ago
Yes but api errors like 400 or 500 will not be caught
correct-apricot
correct-apricotOP2y ago
ah~ thankyou If I add this code, it works. right?
if(!res.ok) throw Error("~~");
if(!res.ok) throw Error("~~");
conscious-sapphire
conscious-sapphire2y ago
Looks good to me

Did you find this page helpful?