-
Notifications
You must be signed in to change notification settings - Fork 43
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
[최제원] Sprint6 #245
The head ref may contain hidden characters: "React-\uCD5C\uC81C\uC6D0-sprint6"
[최제원] Sprint6 #245
Conversation
This reverts commit e46d37a.
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.
고생하셨습니다!!
src/Api.js
Outdated
export async function productList({ orderBy = "recent" }) { | ||
const query = `page=1&pageSize=10&orderBy=${orderBy}`; | ||
const response = await fetch( | ||
`https://panda-market-api.vercel.app/products?${query}` | ||
); | ||
if (!response.ok) { | ||
throw new Error("제품목록을 가지고 오는데 실패했습니다."); | ||
} | ||
const date = await response.json(); | ||
return date; | ||
} |
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.
export async function productList({
orderBy = "recent",
page = 1,
pageSize = 10,
} = {}) {
try {
// Query parameters 구성
const params = new URLSearchParams({
page: String(page),
pageSize: String(pageSize),
orderBy,
});
// API 호출
const response = await fetch(
`https://panda-market-api.vercel.app/products?${params}`
);
// 응답 상태 확인
if (!response.ok) {
throw new Error(
`Failed to fetch product list: ${response.status} ${response.statusText}`
);
}
// JSON 데이터 파싱
const data = await response.json();
return data;
} catch (error) {
console.error("Error fetching product list:", error);
throw error; // 필요시 상위 호출부에서 에러 핸들링
}
}
이렇게 바꿔보시는건 어떤가요? ㅎ orderBy
를 파라미터로 받는데 page와 pageSize를 받지 않을 이유는 없을것 같아요 ㅎ
추가로 에러에 대해 보다 유연한 처리등을 도입하면 더 좋을것 같습니다 ㅎ
throw new Error("제품목록을 가지고 오는데 실패했습니다.");
는 직관적이기는 하나 서버에서 내려주는 에러를 버리고 프론트엔드에서 에러를 다시 구성한다는 치명적인 단점을가지고 있어요
api request에서 발생하는 에러에 대한 이해도는 서버가 항상 더 많이 가지고 있기때문에 최대한 서버의 response를 활용하는것이 좋답니다
src/components/App.js
Outdated
const loadAllItems = async () => { | ||
try { | ||
const { list } = await productList({ orderBy: "recent" }); | ||
setAllItems(list); | ||
} catch (error) { | ||
console.error("전체 상품 데이터를 불러오는 중 오류 발생:", error.message); | ||
} | ||
}; |
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 await 사용하신것 좋습니다 ㅎㅎ
다만 Try catch는 productList 함수 내부에서 처리하는게 좀더 목적성을 명확히 나누는 코드가 될 것 같네요 ㅎ
src/components/App.js
Outdated
useEffect(() => { | ||
loadAllItems(); | ||
loadBestItems(); | ||
}, []); |
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
를 사용하실 땐 왜 사용하는지에 대해서 주석을 남겨주면 좋아요!
src/components/ItemList.js
Outdated
return ( | ||
<ul> | ||
{items.map((item) => { | ||
console.log(item); |
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.
이건 아마 개발하다 끼어들어간 console.log인것 같은데 PR올릴때 이런건 다 지워주세요!
추후 미래를위한 좋은 습관으로 ㅎㅎ
src/App.jsx
Outdated
path="/" | ||
element={<Navigate to="/items" replace></Navigate>} | ||
></Route> | ||
<Route path="/items" element={<Home></Home>}></Route> |
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.
<Home></Home>
-> <Home/>
src/pages/Home/Home.jsx
Outdated
<form className="all-products__search-container"> | ||
<input | ||
type="text" | ||
placeholder="검색할 상품을 입력해주세요" | ||
className="all-products__input" | ||
/> | ||
<Link to="/additem" className="all-products__add-button"> | ||
상품 등록하기 | ||
</Link> | ||
<select | ||
value={selectedOption} | ||
onChange={handleOptionClick} | ||
className="dropDown-products" | ||
> | ||
<option value="최신순">최신순</option> | ||
<option value="인기순">인기순</option> | ||
</select> | ||
</form> |
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.
form에 submit 버튼이 없네요?
요구사항
기본
심화
주요 변경사항
스크린샷
멘토에게