Last active
February 14, 2024 08:04
-
-
Save ravisiyer/2a4257540a124aedd10a3ef6ead736aa to your computer and use it in GitHub Desktop.
useAxiosGet custom hook for axios get requests only: version with cleanupAbort and axiosGetInProgress variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "./App.css"; | |
import { useEffect, useState } from "react"; | |
import Home from "./Home"; | |
// lines snipped | |
import useAxiosGet from "./hooks/useAxiosGet"; | |
function App() { | |
const [posts, setPosts] = useState([]); | |
const [arePostsLoading, setArePostsLoading] = useState(true); | |
const [searchPosts, setSearchPosts] = useState(""); | |
const { | |
data, | |
isLoading: isDataLoading, | |
axiosGetError, | |
} = useAxiosGet("http://localhost:3500/Posts"); | |
useEffect(() => { | |
if (!isDataLoading) { | |
setPosts(data); | |
setArePostsLoading(false); | |
} | |
}, [data, isDataLoading]); | |
return ( | |
// lines snipped | |
<Route index element={ | |
<Home | |
posts={ | |
searchPosts.trim().length === 0 ? posts : posts.filter((post) => { | |
return (post.title.toLowerCase().includes(searchPosts.toLowerCase()) || | |
post.body.toLowerCase().includes(searchPosts.toLowerCase())); | |
}) | |
} | |
isLoading={arePostsLoading} | |
axiosGetError={axiosGetError} | |
/> | |
} | |
/> | |
// lines snipped |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from "axios"; | |
import { useState, useEffect } from "react"; | |
const useAxiosGet = (url) => { | |
const [data, setData] = useState([]); | |
const [isLoading, setIsLoading] = useState(true); | |
const [axiosGetError, setAxiosGetError] = useState(""); | |
useEffect(() => { | |
const controller = new AbortController(); | |
let cleanupAbort = false; | |
let axiosGetInProgress = false; | |
const getAxiosData = async () => { | |
try { | |
setIsLoading(true); | |
axiosGetInProgress = true; | |
const res = await axios.get(url, { | |
signal: controller.signal, | |
}); | |
axiosGetInProgress = false; | |
setData(res.data); | |
setAxiosGetError(""); | |
} catch (error) { | |
if (cleanupAbort) { | |
cleanupAbort = false; // Code works even if this line is commented but | |
// am retaining it to be on safe side. | |
} else { | |
setAxiosGetError(error.message); | |
setData([]); | |
} | |
} finally { | |
setIsLoading(false); | |
} | |
}; | |
const cleanup = () => { | |
if (axiosGetInProgress && controller) { | |
cleanupAbort = true; | |
controller.abort(); | |
} | |
}; | |
getAxiosData(); | |
return cleanup; | |
}, [url]); | |
return { data, isLoading, axiosGetError }; | |
}; | |
export default useAxiosGet; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment