-
Notifications
You must be signed in to change notification settings - Fork 0
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
[ 4주차 기본과제 ] 👨🏻💻 깃헙 프로필 검색하기 #8
Conversation
[ 1주차 기본/심화/생각 과제 ] ✅ TodoList, 💻 velog 클론코딩 , 📝 웹 접근성
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 default function SearchPage() { | ||
const [username, setUsername] = useState(""); // input 값 | ||
const [focus, setFocus] = useState(false); // input 포커싱 상태 | ||
let [history, setHistory] = useState([]); // 검색 히스토리를 저장할 배열 |
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 [history, setHistory] = useState(JSON.parse(localStorage.getItem('history') || '[]'));
이렇게 한 번에 상태 관리하는 것은 어떨까?
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.
오오 이렇게 하면 따로 initHistory()로 초기화를 안해줘도 되겠구나!!! 넘 좋은거 배워가용!!!
const handleChange = (e) => setUsername(e.target.value); | ||
|
||
// 검색 클릭 시 history 배열에 넣고 로컬스토리지에 저장 | ||
const handleSubmit = async (e) => { |
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.
await
어디갔어 !!!
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 onClickDelete = (_history) => { | ||
const num = history.indexOf(_history); | ||
setHistory(history.splice(num, 1)); |
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에서 불변성을 지켜주는 것이 핵심인데, splice
메서드를 사용하면 지켜지지 않아!
https://devbksheen.tistory.com/258
setHistory((prev) => prev.filter((_, index) => index !== num))
이렇게 forEach
메서드를 활용하면 좋을 것 같아!
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.
@YOOJS1205 리액트 불변성도 생각하지 않고 막 갈긴 내 코드........ 한없이부끄럽다........................... 필터를 사용해서 수정 완료!!!!
); | ||
} | ||
|
||
// -------------------------- style -------------------------- |
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.
정확한 기준이 있는 것은 아닌데, 코드가 100줄 이상 넘어가면
스타일 코드를 다른 파일로 빼는 방법도 좋을 것 같아
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.
오오 그렇구나!!! 관련해서 한번 더 찾아봐야게따!!! 고마워 준상!!!~~~
); | ||
setUsers(response.data); | ||
setLoading(false); | ||
}; |
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.
try
catch
문을 활용해서 예외처리를 해주면 에러가 떴을 때 디버깅하기 훨씬 용이해져서 좋을 것 같아.
https://ko.javascript.info/try-catch
const getUser = async (username) => {
try {
// 시도할 코드들
} catch (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.
try catch 문으로 예외처리도 추가해줘따!!! 관련 자료까지 ㅠㅠㅠ 준상 넘 고마워 ㅠㅠㅠㅠㅠㅠㅠ @YOOJS1205
✨ 구현 기능 명세
기본 과제
react-router-dom
을 이용해 url 에 따른 컴포넌트를 나타낸다/search
에서, 검색창 및 검색 history 를 포함한 내용을 나타낸다/search/:userId
에서, userId 에 맞는 유저 정보를 받아온다 (GET 통신)/search/:userId
로 이동한다🎁 PR Point
😭 소요 시간, 어려웠던 점
3일
😎 구현 결과물