Usage
import React, { useState } from "react";
import { useAxios } from "./use-axios";
const App = () => {
const [id, setId] = useState("1");
const axiosConfig = { method: "get", timeout: 2500 };
const { isLoading, isError, response } = useAxios(
`https://pokeapi.co/api/v2/pokemon/${id}`,
axiosConfig
);
return (
{response?.data && <div>{data}</div>}
{isLoading && <LoadingIcon/>}
{isError && <ErrorMsg/>}
);
};
Overview
useAxios
is an Axios-specific implementation of my generic useAsyncFunc React hook.
One issue for async operations is when the return value is no longer required. For example, the user leaves the page (the requesting component is unmounted) or the user provides a new search query (the old search query's response is superfluous).
You might see an error like this:
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
Conclusion
It is good to cancel superfluous requests so that they do not become memory leaks! I hope you find this example helpful.