-
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
[최영선] Sprint8 #242
The head ref may contain hidden characters: "React-\uCD5C\uC601\uC120-sprint8"
[최영선] Sprint8 #242
Conversation
- 커스텀 훅 사용해서 commentlist 수정 - 오타 수정 - formatRelativeTime 유틸로 분리 - API_BASE_URL 상수로 변경
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 { useState, useCallback, useEffect } from "react"; | ||
|
||
const useFetch = (fetchCallback, dependencies = []) => { | ||
const [data, setData] = useState(null); | ||
const [error, setError] = useState(null); | ||
const [loading, setLoading] = useState(true); | ||
|
||
const fetchData = useCallback(async () => { | ||
setLoading(true); | ||
try { | ||
const result = await fetchCallback(); | ||
setData(result); | ||
setError(null); | ||
} catch (error) { | ||
setError(error.message); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}, dependencies); | ||
|
||
useEffect(() => { | ||
fetchData(); | ||
}, [fetchData]); | ||
|
||
return { data, error, loading }; | ||
}; | ||
|
||
export default useFetch; |
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.
useFetch
좋아요! 파트 2때, 사용하신 react-query
내 useQuery
가 정말 편하게 많은걸 해준다는걸 느끼셨죠!? 이렇게 직접 만들면서 사용해야하거든요~
<StyledNavContainer> | ||
<FrontNavContainer> | ||
<Link to={`/`}> | ||
<StyledLogo src={pandaIcon} alt="판다마켓 로고" /> | ||
</Link> | ||
<div> | ||
<StyledLink href="https://www.google.com/">자유게시판</StyledLink> | ||
<StyledLink to={`/market`}>중고마켓</StyledLink> | ||
</div> | ||
</FrontNavContainer> | ||
<StyledButton>로그인</StyledButton> | ||
</StyledNavContainer> |
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.
확실히 이전보다 styled-components
숙련도가 올라왔네요! 👍
border: none; | ||
border-radius: 50%; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
cursor: pointer; | ||
|
||
&:hover { | ||
background-color: var(--blue-100); | ||
} | ||
`; | ||
|
||
const StyledFileArea = styled.div` | ||
display: flex; | ||
justify-content: flex-start; | ||
gap: 20px; | ||
`; | ||
|
||
const StyledImgArea = styled.div` | ||
position: relative; | ||
display: inline-block; | ||
`; | ||
|
||
function FlieInput({ name, value, onChange }) { |
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.
해당 파일처럼 스타일링 관련된 컴포넌트가 많아서, 중요한 컴포넌트에 관심을 가지기 어려운 상태가 있을 수 있어요.
그렇다면, 파일을 나누는 방법이 있어요.
FileInput 디렉토리를 하나 만들고, 그 안에 style만 하는 파일과, 로직이 담긴 컴포넌트를 2개로 나누는 방법이예요.
ㄴ AddItems (폴더)
ㄴ AddItemPage.css
ㄴ AddItemPage.js
ㄴFileInput (폴더)
ㄴ FileInput.js
ㄴ FileInput.styled.js
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 React from "react";
import {
StyledLabel,
StyledPreviewImg,
StyledFileInputBox,
StyledFileInputPlaceholder,
StyledFileInput,
StyledCancelButton,
StyledFileArea,
StyledImgArea,
} from "./FileInput.styled";
function FileInput({ name, value, onChange }) {
return (
<div>
...
</div>
);
}
export default FileInput;
const { | ||
data: comments, | ||
error, | ||
loading, | ||
} = useFetch(async () => { | ||
const result = await getCommentById(productId); | ||
return result.list; | ||
}, [productId]); |
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 호출 로직을 별도로 분리하는 방법이 있어요.
그러면 결국 useComments
hook을 만들어서 다른곳에서도 재사용 가능하도록 할 수 있구요.
가독성도 좋아지고, 유지보수에도 좋아요.
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.js
export const getComments = async (productId) => {
try {
const result = await getCommentById(productId);
return result.list;
} catch (error) {
console.error(error);
throw error;
}
};
// useComments.js
import { useFetch } from './useFetch';
import { getComments } from './api';
export const useComments = (productId) => {
return useFetch(() => getComments(productId), [productId]);
};
그럼 최종적으로 아래처럼 바꿀 수 있어요.
// 컴포넌트에서 사용
const { data: comments, error, loading } = useComments(productId);
const StyledProductSection = styled.section` | ||
display: grid; | ||
grid-template-columns: 2fr 3fr; | ||
grid-template-areas: "image detail"; | ||
gap: 24px; | ||
`; |
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.
grid 사용 좋아요👍
배포 사이트
https://pandamarket-choiys.netlify.app/
요구사항
기본
주요 변경사항
스크린샷
AddItems
Productdetail
호버, x버튼 구현 동영상
아이템리스트
멘토에게