Skip to content

Commit

Permalink
refactor: (#123) 중복되는 기능 함수 합치기
Browse files Browse the repository at this point in the history
  • Loading branch information
chsua committed Jul 25, 2023
1 parent 568d074 commit 47aa761
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 43 deletions.
26 changes: 14 additions & 12 deletions frontend/src/hooks/useFetch.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import { useEffect, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';

export const useFetch = <T>(fetchFn: () => Promise<T>) => {
const [data, setData] = useState<T>();
const [errorMessage, setErrorMessage] = useState<string>();
const [data, setData] = useState<T | null>(null);
const [errorMessage, setErrorMessage] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(true);

useEffect(() => {
const refetch = useCallback(() => {
setIsLoading(true);
setData(null);
setErrorMessage(null);

fetchFn()
.then(res => {
setData(res);
})
.catch(rej => {
setErrorMessage(rej.message);
.catch(error => {
setErrorMessage(error.message);
})
.finally(() => {
setIsLoading(false);
});
}, [fetchFn]);

return (() => {
setData(undefined);
setIsLoading(true);
setErrorMessage(undefined);
})();
useEffect(() => {
refetch();
}, []);

return { data, errorMessage, isLoading };
return { data, errorMessage, isLoading, refetch };
};
30 changes: 0 additions & 30 deletions frontend/src/hooks/useFetch_2.ts

This file was deleted.

2 changes: 1 addition & 1 deletion frontend/src/pages/post/PostDetail/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useNavigate, useParams } from 'react-router-dom';

import { useFetch } from '@hooks/useFetch_2';
import { useFetch } from '@hooks/useFetch';

import { getPost, removePost, setEarlyClosePost } from '@api/post';

Expand Down

0 comments on commit 47aa761

Please sign in to comment.