Skip to content

Commit

Permalink
feat: useFetch 시 error boundary 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
jhj2713 committed Aug 11, 2024
1 parent c97241b commit b169f3d
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions client/src/hooks/useFetch.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { useState } from "react";
import { useErrorBoundary } from "react-error-boundary";

export default function useFetch<T, P = void>(fetch: (params: P) => Promise<T>) {
const [data, setData] = useState<T | null>(null);
const [isSuccess, setIsSuccess] = useState<boolean>(false);
const [isError, setIsError] = useState<boolean>(false);

const { showBoundary } = useErrorBoundary();

const fetchData = async (params?: P) => {
try {
const data = await fetch(params as P);
setData(data);
setIsSuccess(!!data);
} catch (error) {
showBoundary(error);
setIsError(true);
console.error(error);
}
Expand Down

0 comments on commit b169f3d

Please sign in to comment.