The React Query library lets us make HTTP requests easily in our React apps.
In this article, we’ll look at how to make mutations with React Query?
Mutations
Mutations let us make HTTP requests to change data on a server by creating, updating, and deleting them.
For instance, we can write:
index.js
import { StrictMode } from "react";
import ReactDOM from "react-dom";
import { QueryClient, QueryClientProvider } from "react-query";
import App from "./App";
const queryClient = new QueryClient();
const rootElement = document.getElementById("root");
ReactDOM.render(
<QueryClientProvider client={queryClient}>
<StrictMode>
<App />
</StrictMode>
</QueryClientProvider>,
rootElement
);
App.js
import axios from "axios";
import React from "react";
import { useMutation } from "react-query";
export default function App() {
const { mutate, isLoading, isError, isSuccess } = useMutation((data) =>
axios.post("https://jsonplaceholder.typicode.com/posts", data)
);
return (
<div>
<div>{isLoading && "loading"}</div>
<div>{isError && "error"}</div>
<div>{isSuccess && "success"}</div>
<button
onClick={() => {
mutate({
title: "foo",
body: "bar",
userId: 1
});
}}
>
Add Todo
</button>
</div>
);
}
We call the useMutation
hook with a callback that lets us make a POST request to the API to submit some data.
The callback should return a promise.
The request payload is stored in the payload
parameter.
The hook returns the mutate
function that lets us make the request.
isLoading
is true
when the request is loading.
isError
is true
when a request fails.
isSuccess
is true
when the request is successfully completed.
Alternatively, we can replace isLoading
, isSuccess
, and isError
with the status
property:
import axios from "axios";
import React from "react";
import { useMutation } from "react-query";
export default function App() {
const { mutate, status } = useMutation((data) =>
axios.post("https://jsonplaceholder.typicode.com/posts", data)
);
return (
<div>
<div>{status === "loading" && "loading"}</div>
<div>{status === "error" && "error"}</div>
<div>{status === "success" && "success"}</div>
<button
onClick={() => {
mutate({
title: "foo",
body: "bar",
userId: 1
});
}}
>
Add Todo
</button>
</div>
);
}
'loading'
status is the status when the request is loading.
'error'
status is the status when the request has an error.
'success'
status is the status when the request is successful.
Resetting Mutation State
We can clear the mutation request state after the request is done.
To do this, we can call the reset
method:
import axios from "axios";
import React, { useState } from "react";
import { useMutation } from "react-query";
export default function App() {
const { reset, mutate } = useMutation((data) =>
axios.post("https://jsonplaceholder.typicode.com/posts", data)
);
const [title, setTitle] = useState("");
const onCreateTodo = (e) => {
e.preventDefault();
mutate({
title
});
};
return (
<div>
<form onSubmit={onCreateTodo}>
<input
type="text"
value={title}
onChange={(e) => setTitle(e.target.value)}
/>
<br />
<button type="submit">Create Todo</button>
<button type="button" onClick={() => reset()}>
reset
</button>
</form>
</div>
);
}
We call the reset
method when we click on the reset the state returned by the useMutation
hook.
Conclusion
We can make requests that change data on the server with the React Query useMutation
hook.