-
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
[김보미] sprint11 #670
The head ref may contain hidden characters: "Next.js-\uAE40\uBCF4\uBBF8-sprint11"
[김보미] sprint11 #670
Conversation
import axios from "axios"; | ||
|
||
const axiosGetInstance = axios.create({ | ||
baseURL: process.env.NEXT_PUBLIC_BASE_URL, | ||
}); | ||
|
||
export default axiosGetInstance; |
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.
한 파일에서 동일한 instance를 사용하면 에러가 나더라구요.. POST요청을 할 때 인터셉터를 설정해야 해서, GET instance를 분리했습니다.
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.
오잉 혹시 어떤 문제가 있었던가요 ?
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.
Failed to load resource: the server responded with a status of 404 (Not Found)
boards/id로 이동하면 404페이지 뜹니다... 따로 분리하니까 잘 들어가지더라구요,,, 왜 오류나는지는 모르겠습니돠..! ㅠ
useEffect(() => { | ||
const token = localStorage.getItem("accessToken"); | ||
setAccessToken(token); | ||
}, []); | ||
if (token) { | ||
setAccessToken(token); | ||
} | ||
}, [router.pathname]); |
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.
전에 로그인 후 바로 적용이 안 되는 이슈가 있어서 reload()를 썼었는데 의존성 배열에 router.pathname을 넣으면 바로 반영되도록 수정했습니다.
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.
이렇게 되면 리다이렉트 시 변경을 감지하고 리렌더링 시켜주는 걸까요?
일단 논리적으로는 맞는 말이지만 근본적이진 못할 수 있을 것 같아요.
Context API를 통하여 헤더가 상태를 관리하고 있다면 어떨까요?
const pages = useMemo( | ||
() => Array.from({ length: totalPages }, (_, i) => i + 1), | ||
[totalPages], | ||
); |
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.
멘토님께서 저번 미션 때 메모이제이션을 추천해주셔서 처음으로 사용해봤습니다. 사실, 메모이제이션에 대해 깊이 이해하지는 못했고, 리액트도 아직 익숙하지 않아서 메모이제이션이 다소 어렵게 느껴졌습니다. 언제 사용하는 것이 좋을지 판단하는 것도 쉽지 않네요..ㅎㅎ..
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.
ㅎㅎㅎ 그쵸. 다음 아티클을 읽어보세요 !
[https://kentcdodds.com/blog/usememo-and-usecallback](useMemo and useCallback)
수고 하셨습니다 ! 스프리트 미션 하시느라 정말 수고 많으셨어요. |
저번 미션과 이어지는 미션(로그인 유무에 따라 게시글, 댓글 등록하기)이라 Next.js로 구현했습니다.넵넵! 좋습니다 😊 |
const params = new URLSearchParams({ | ||
pageSize: "10000", | ||
orderBy: "recent", | ||
}); | ||
|
||
const { data } = await axiosGetInstance.get("/articles?", { params }); |
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.
굳굳 ! 훨신 깔끔해졌네요 😊
const params = new URLSearchParams({ | |
pageSize: "10000", | |
orderBy: "recent", | |
}); | |
const { data } = await axiosGetInstance.get("/articles?", { params }); | |
const params = new URLSearchParams({ | |
pageSize: "10000", | |
orderBy: "recent", | |
}); | |
const { data } = await axiosGetInstance.get("/articles", { params }); |
위와 같이 ?
는 지워도 될 것으로 보여요 !
headers: { | ||
"Content-Type": "multipart/form-data", | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}); |
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.
여기만 Content-Type
이 달라서 이렇게 처리하셨군요 👍👍
} catch (error) { | ||
console.error("postArticleComments 함수에서 오류 발생:", error); | ||
} |
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.
오잉? 이렇게 되면 함수 반환타입이 void
이거나 any
가 될 것으로 보입니다 !
} catch (error) { | |
console.error("postArticleComments 함수에서 오류 발생:", error); | |
} | |
} catch (error) { | |
console.error("postArticleComments 함수에서 오류 발생:", error); | |
throw error; | |
} |
만약 에러가 발생하면 컴포넌트에서 캐치하여 처리해야하지 않을까요?
const { data } = await axiosGetInstance.get( | ||
`/articles?&pageSize=${pageSize}`, | ||
{ | ||
params, | ||
}, |
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.
반환 타입도 지정해보면 어떨까요?
const { data } = await axiosGetInstance.get( | |
`/articles?&pageSize=${pageSize}`, | |
{ | |
params, | |
}, | |
const { data } = await axiosGetInstance.get<GetBestPosts>( | |
`/articles?&pageSize=${pageSize}`, | |
{ | |
params, | |
}, |
useEffect(() => { | ||
const token = localStorage.getItem("accessToken"); | ||
setAccessToken(token); | ||
}, []); | ||
if (token) { | ||
setAccessToken(token); | ||
} | ||
}, [router.pathname]); |
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.
이렇게 되면 리다이렉트 시 변경을 감지하고 리렌더링 시켜주는 걸까요?
일단 논리적으로는 맞는 말이지만 근본적이진 못할 수 있을 것 같아요.
Context API를 통하여 헤더가 상태를 관리하고 있다면 어떨까요?
|
||
// accessToken이 존재하는 경우, 헤더에 추가 | ||
if (accessToken) { | ||
config.headers["Content-Type"] = "application/json"; |
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.
Content-Type
은 처음 createInstance
하실 때 넣어두셔도 됩니다 !
import axios from "axios"; | ||
|
||
const axiosGetInstance = axios.create({ | ||
baseURL: process.env.NEXT_PUBLIC_BASE_URL, | ||
}); | ||
|
||
export default axiosGetInstance; |
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.
오잉 혹시 어떤 문제가 있었던가요 ?
const { | ||
register, | ||
handleSubmit, | ||
watch, | ||
formState: { errors, isValid }, | ||
} = useForm<FormData>({ | ||
mode: "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.
굳굳 ! react hook form을 사용하셨군요 !
훌륭한 비제어 컴포넌트 툴이지요 ! 👍
main: "#3692ff", | ||
error: "#f74747", | ||
gray: { | ||
50: "#f7f7f8", | ||
100: "#e8ebed", | ||
200: "#e5e7eb", | ||
400: "#9ea4a8", | ||
500: "#72787f", | ||
600: "#454c53", | ||
700: "#374151", | ||
800: "#26282b", | ||
900: "#1b1d1f", | ||
}, | ||
coolgray: { | ||
50: "#f9fafb", | ||
100: "#f3f4f6", | ||
800: "#1f2937", | ||
}, | ||
btn: { | ||
1: "#3692ff", | ||
2: "#1967d6", | ||
3: "#1251aa", | ||
4: "#9ca3af", | ||
}, | ||
bannerbg: "#cfe5ff", | ||
loginbg: "#e6f2ff", | ||
footer: "#111322", | ||
footercodeit: "#676767", | ||
borderbottom: "#e5e7eb", | ||
fontcolor: "#6b7280", |
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.
굳굳 ! 테마를 이번 시간에 다 정의해두셨군요 !
앞으로 브랜딩 컬러를 사용할 때 정말 유용하겠어요 !
gray: { | ||
50: "#f7f7f8", | ||
100: "#e8ebed", | ||
200: "#e5e7eb", | ||
400: "#9ea4a8", | ||
500: "#72787f", | ||
600: "#454c53", | ||
700: "#374151", | ||
800: "#26282b", | ||
900: "#1b1d1f", | ||
}, |
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.
이렇게 정의할 수도 있었군요 🤔🤔
덕분에 하나 알아갑니다 ! 👍
보미님 코드를 보면 항상 깔끔하고 읽기가 쉬운게 느껴져요 ㅎㅎㅎ |
요구사항
기본
회원가입
로그인
메인
심화
주요 변경사항
스크린샷
멘토에게