Skip to content

Commit

Permalink
Feat : 로딩,에러 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
kjh9852 committed Aug 12, 2024
1 parent b84de6d commit f089662
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions hooks/usePostList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react";
import { useState } from 'react';

interface queryType {
query: {
Expand All @@ -12,18 +12,28 @@ export default function usePostList<T>(
getFn: (query: queryType) => Promise<T[]>,
initialList: T[]
) {
const [loading, setLoading] = useState<Boolean>(false);
const [error, setError] = useState<string | null>(null);
const [dataList, setDataList] = useState<T[]>(initialList);
const fetchPost = async (query: queryType) => {
setLoading(true);
try {
const res = await getFn(query);
console.log(res);
setDataList(res);
} catch (err) {
throw new Error("데이터를 불러오는데 실패했습니다.");
} catch (error) {
if (error instanceof Error) {
setError(error.message);
} else {
setError('예기치 않은 오류가 발생했습니다.');
}
} finally {
setLoading(false);
}
};

return {
loading,
error,
dataList,
fetchPost,
};
Expand Down

0 comments on commit f089662

Please sign in to comment.