-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ba32e1
commit e428650
Showing
4 changed files
with
202 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { useState, useCallback, useMemo } from "react"; | ||
import axios from "axios"; | ||
import queryString from "query-string"; | ||
import UseAxiosProps from "../types/UseAxiosProps.types"; | ||
|
||
const useAxios = ({ | ||
url, | ||
method = "GET", | ||
headers, | ||
body, | ||
searchParams, | ||
}: UseAxiosProps) => { | ||
const [data, setData] = useState<any | null>(null); | ||
const [loading, setLoading] = useState(false); | ||
const [error, setError] = useState(false); | ||
|
||
const memoizedSearchParams = useMemo(() => { | ||
return queryString.parse(searchParams || ""); | ||
}, [searchParams]); | ||
|
||
const runAxios = useCallback(async () => { | ||
try { | ||
setLoading(true); | ||
|
||
const response = await axios({ | ||
method, | ||
url, | ||
data: body, | ||
params: memoizedSearchParams, | ||
headers, | ||
}); | ||
|
||
setData(response.data); | ||
} catch (error) { | ||
setError(!!error); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}, [method, url, headers, body,memoizedSearchParams]); | ||
|
||
return { runAxios, data, loading, error }; | ||
}; | ||
|
||
export default useAxios; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { AxiosRequestConfig } from "axios"; | ||
|
||
type UseAxiosProps = { | ||
url: string; | ||
method?: string; | ||
headers?: AxiosRequestConfig["headers"]; | ||
body?: any; | ||
searchParams?: string | null; | ||
}; | ||
|
||
export default UseAxiosProps; |