The React Query library lets us make HTTP requests easily in our React apps.
In this article, we’ll look at how to make HTTP requests with React Query.
mutateAsync
The mutateAsync
method lets us call mutate
is an async manner.
It returns a promise which lets us commit our mutation request in an async manner, which doesn’t hold up the JavaScript main thread.
For instance, we can write:
import axios from "axios";
import React, { useState } from "react";
import { useMutation } from "react-query";
export default function App() {
const { reset, mutateAsync } = useMutation((data) =>
axios.post("https://jsonplaceholder.typicode.com/posts", data)
);
const [title, setTitle] = useState("");
const onCreateTodo = async (e) => {
e.preventDefault();
try {
const todo = await mutateAsync({
title
});
console.log(todo);
} catch (error) {
console.log(error);
} finally {
console.log("done");
}
};
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 mutateAsync
which returns a promise with the response data from the axios.post
call.
Retry Mutations
With React Query, we can easily retry our mutation HTTP request if it returns an error.
We just have to set the retry
option to the number of times we want to retry.
For instance, we can write:
import axios from "axios";
import React, { useState } from "react";
import { useMutation } from "react-query";
export default function App() {
const { reset, mutateAsync } = useMutation(
(data) => axios.post("https://jsonplaceholder.typicode.com/posts", data),
{
retry: 3
}
);
const [title, setTitle] = useState("");
const onCreateTodo = async (e) => {
e.preventDefault();
try {
const todo = await mutateAsync({
title
});
console.log(todo);
} catch (error) {
console.log(error);
} finally {
console.log("done");
}
};
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 useMutation
hook with an object that has the retry
property set to 3 to retry up to 3 times if the mutation request fails.
Invalidate Queries
We can invalidate queries so we can mark a query request as stale and make the request again automatically.
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();
queryClient.invalidateQueries("yesNo", { exact: true });
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 { useQuery } from "react-query";
export default function App() {
const { data } = useQuery("yesNo", () => axios("https://yesno.wtf/api"));
return <div>{JSON.stringify(data)}</div>;
}
We call:
queryClient.invalidateQueries("yesNo", { exact: true });
to invalidate the query by the key.
exact
set to true
means the key of the query request must match exactly before it’s invalidated.
Conclusion
We run mutation requests asynchronously and invalidate query requests to make the request again with Reacr Query.