-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[오영택] sprint 5,9 #704
base: main
Are you sure you want to change the base?
The head ref may contain hidden characters: "Next.js-\uC624\uC601\uD0DD-sprint11"
[오영택] sprint 5,9 #704
Conversation
}) | ||
|
||
try { | ||
const res = await fetch( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
공통 api 함수가 하나 있으면 좋겠네요.
아래 baseApi 함수를 확인해주시면 됩니다.
type BaseApiMethods = "get" | "post" | "put" | "patch" | "delete";
type BaseApiCommonArgs = {
url: URL;
headers: RequestInit["headers"];
};
interface BaseApiNonBodyArgs extends BaseApiCommonArgs {
method: "get" | "delete";
}
interface BaseApiWithBodyArgs extends BaseApiCommonArgs {
method: "post" | "put" | "patch";
body: RequestInit["body"];
}
type BaseApiArgs<T extends BaseApiMethods> = T extends "get"
? BaseApiNonBodyArgs
: BaseApiWithBodyArgs;
type BaseApiArgsType = BaseApiNonBodyArgs | BaseApiWithBodyArgs;
type BaseApiFunc = <T extends BaseApiArgsType["method"]>(
data: BaseApiArgs<T>
) => Promise<Response>;
const API_BASE_URL = "https://panda-market-api.vercel.app";
export const baseApi: BaseApiFunc = async ({ url, ...args }) => {
try {
const timeout = AbortSignal.timeout(5000); // timeout 5초
return await fetch(`${API_BASE_URL}/${url}`, { ...args, signal: timeout });
}
catch(err) {
// 실패했을 때 공통적으로 return 해줘야 할 응답값
}
};
|
||
const viewportWidth = useViewport(); | ||
|
||
useEffect(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useEffect
는 react의 여러 라이프사이클을 핸들링 할 수 있는 훅입니다.
그래서 내부에서 추가적으로 호출해줘야되는 함수들이 늘어날 수 있기 때문에, 함수의 구현부(fetchBestArticles
)는 useEffect
외부로 빼주시면 좋습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
전반적으로 기본적인 기능 구현사항이라 크게 커멘트를 남길 필요는 없을 듯 합니다.
그리고 현재 각 컴포넌트에서 api 호출 후, 응답 데이터를 state로 관리하여 ui 렌더링 시키는 구조가 많은 데,
setLoading(true) -> api response -> setLoading(false) -> setData
이런 반복적인 코드가 보이네요.
type UseApiArgs {
// .....
}
// useApi 내부에서 실제 api 콜을 하는 함수
type Fetcher {
// ....
}
const useApi = <T>(fetcher: Fetcher) => {
const [data, setData] = useState<T>();
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState();
const trigger = async () => {
try{
setIsLoading(true);
const result = await fetcher();
setIsLoading(false;
setData(result)
}
catch(err) {
setError(err);
}
}
return {
data, isLoading, error, trigger
}
}
// ./page or ./component
const fetcher = () => {
return fetch("", {});
}
const DummyComponent = () => {
const {data, isLoading, error, trigger} = useApi(fetcher);
return (
<>
{isLoading && <Loading />}
{!isLoading && <div>{JSON.stringify(data)}</div>
</>
)
}
요구사항
기본
<미션 9>
<미션 5> 넥스트로 구현
심화
<미션 9>
<미션 5>
주요 변경사항
스크린샷
멘토에게
-tailwind 동적 CSS 구현방법을 잘 모르겠습니다...