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.
Query Retries
The useQuery
hook will retry requests automatically if they fail.
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 { useQuery } from "react-query";
export default function App() {
const { data } = useQuery(
["todo", 1],
({ queryKey: [, id] }) => {
return axios(`https://jsonplaceholder.typicode.com/posts/${id}`);
},
{ retry: 3 }
);
return (
<div>
<div>{JSON.stringify(data)}</div>
</div>
);
}
We set retry
to 3 to retry the request 3 times if it fails.
Also, we can set retry
to true
to retry an infinite number of times.
And we can set retry
to false
to never retry.
Retry Delay
We can also set a retry delay with the retryDelay
property.
The value should be in milliseconds.
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({
defaultOptions: {
queries: {
retryDelay: (attemptIndex) => Math.min(1000 * 2 ** attemptIndex, 30000)
}
}
});
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(["todo", 1], ({ queryKey: [, id] }) => {
return axios(`https://jsonplaceholder.typicode.com/posts/${id}`);
});
return (
<div>
<div>{JSON.stringify(data)}</div>
</div>
);
}
In index.js
, we have the retryDelay
method that takes the attemptIndex
which is the attempt number being made minus 1.
And we return the number of milliseconds before another retry is made if a request fails.
Also, we can override the retry interval for a single request with the retryDelay
property in the options we pass into the useQuery
hook:
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 { useQuery } from "react-query";
export default function App() {
const { data } = useQuery(
["todo", 1],
({ queryKey: [, id] }) => {
return axios(`https://jsonplaceholder.typicode.com/posts/${id}`);
},
{
retryDelay: 1000
}
);
return (
<div>
<div>{JSON.stringify(data)}</div>
</div>
);
}
Conclusion
We can set query retry interval easily with React Query.