-
Notifications
You must be signed in to change notification settings - Fork 35
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
[최영선] sprint10 #290
The head ref may contain hidden characters: "Next-\uCD5C\uC601\uC120-sprint9"
[최영선] sprint10 #290
Conversation
…ithub-actions [Fix] delete merged branch github action
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.
과제 제출하시느라 수고하셨습니다~ 👍
import { ChangeEvent, useEffect, useRef, useState } from "react"; | ||
import { | ||
StyledLabel, | ||
StyledPreviewImg, | ||
StyledFileInputBox, | ||
StyledFileInputPlaceholder, | ||
StyledFileInput, | ||
StyledCancelButton, | ||
StyledFileArea, | ||
StyledImgArea, | ||
} from "./FileInput.styled"; | ||
|
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.
지난번 리뷰 사항 반영 굳! 👍
<StyledFileInputBox> | ||
<StyledFileInput | ||
type="file" | ||
accept="image/png, image/jpeg" |
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.
👍
async function getProducts() { | ||
const query = { | ||
orderBy: "like", | ||
page: 1, | ||
pageSize: 3, | ||
}; | ||
const res = await axios.get( | ||
`/articles?orderBy=${query.orderBy}&page=${query.page}&pageSize=${query.pageSize}` | ||
); | ||
const nextArticles = res.data.list; | ||
setArticles(nextArticles); | ||
} |
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.
이 부분은 아래 코드에서도 추가적으로 2번 재사용이 되더라구요~ 아래처럼 모듈로 빼면 더 좋겠어요!
async function fetchArticles(query: ArticleQuery): Promise<Article[]> {
const queryString = new URLSearchParams({
orderBy: query.orderBy,
page: query.page.toString(),
pageSize: query.pageSize.toString(),
...(query.keyword && { keyword: query.keyword }),
}).toString();
const res = await axios.get<ArticleResponse>(`/articles?${queryString}`);
return res.data.list;
}
useEffect(() => { | ||
getProducts(); | ||
}, []); |
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(() => { | |
getProducts(); | |
}, []); | |
useEffect(() => { | |
async function getBestPosts() { | |
try { | |
const bestArticles = await fetchArticles({ | |
orderBy: "like", | |
page: 1, | |
pageSize: 3, | |
}); | |
setArticles(bestArticles); | |
} catch (error) { | |
// 에러 처리 | |
} | |
} | |
getBestPosts(); | |
}, []); |
export async function getServerSideProps(context: any) { | ||
const { orderBy = "recent", keyword = "" } = context.query; | ||
|
||
const query = { | ||
orderBy, | ||
page: 1, | ||
pageSize: 30, | ||
keyword, | ||
}; | ||
const res = await axios.get( | ||
`/articles?orderBy=${query.orderBy}&keyword=${query.keyword}` | ||
); | ||
const initialArticles = res.data.list; | ||
return { | ||
props: { | ||
initialArticles, | ||
initialOrderBy: orderBy, | ||
initialSearchQuery: keyword, | ||
}, | ||
}; | ||
} |
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.
공통 모듈 적용하면 아래처럼 getServerSideProps
에서도 동일하게 사용할 수 있어요~
try {
const initialArticles = await fetchArticles({
orderBy: orderBy as string,
page: 1,
pageSize: 30,
keyword: keyword as string,
});
return {
props: {
initialArticles,
initialOrderBy: orderBy,
initialSearchQuery: keyword,
},
};
} catch (error) {
// 에러 처리
return {
notFound: true,
};
}
initialSearchQuery: string; | ||
} | ||
|
||
export async function getServerSideProps(context: any) { |
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.
import type { GetServerSidePropsContext } from 'next';
...
export async function getServerSideProps(context: GetServerSidePropsContext) {
이렇게 타입을 지정해줄 수 있어요~
요구사항
기본
심화
주요 변경사항
스크린샷
멘토에게