codestates / codestates/HomeTownAlba
[21.11.10] 에러핸들링 (김지윤) - 객체 state 내에 key:value 추가
Open
- Dominant language
- JavaScript
- Stars
- 0
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
## 문제 상황 및 에러 로그
- 상황 : Bracket Notation으로 객체 형태의 state에 key:value를 추가하려고 했으나, 객체가 배열로 바뀜
```js
const openApplicantList = (jobId, idx) => {
axios.get(`http://localhost:5000/applicant/${jobId}`, {withCredentials: true})
.then((res)=> {
setApplicantList(applicantList[idx] = [res.data.data])
})
}
```
## 원인 및 해결방안
- 원인 : 리액트에서 bracket notation은 배열에 값을 할당하는 문법
- 해결방안 : object.assign 또는 객체 구조분해 할당을 사용하여 객체에 값을 추가
```js
setApplicantList({ ...applicantList, [idx]: res.data.data });
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.