-
Notifications
You must be signed in to change notification settings - Fork 35
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
Showing
3 changed files
with
51 additions
and
53 deletions.
There are no files selected for viewing
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,29 @@ | ||
import { useCallback, useEffect, useState } from "react"; | ||
|
||
function useAsync(asyncFunction, arg) { | ||
// pending, error 상태값을 status: '대기중', '로딩중' , '완료', '에러'로 바꾸기 | ||
const [fetchStatus, setFetchStatus] = useState(""); | ||
const [data, setData] = useState({}); | ||
const getFetchData = useCallback(async () => { | ||
setFetchStatus("대기중"); | ||
|
||
try { | ||
const getData = await asyncFunction(arg); | ||
if (!getData) { | ||
throw new Error("데이터를 불러오지 못했습니다."); | ||
} | ||
setData((data) => ({ ...data, getData })); | ||
} catch (error) { | ||
setFetchStatus("에러"); | ||
} finally { | ||
setFetchStatus("완료"); | ||
} | ||
}, [arg, asyncFunction]); | ||
|
||
useEffect(() => { | ||
getFetchData(); | ||
}, [getFetchData]); | ||
|
||
return [fetchStatus, data]; | ||
} | ||
export default useAsync; |
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