-
카테고리
-
세부 분야
프론트엔드
-
해결 여부
미해결
Object.assign으로 값을 복사하여 concat하는 이유가 무엇인가요?
19.12.04 14:08 작성 조회수 226
1
궁금합니다.
답변을 작성해보세요.
1
niobbam
2020.01.13
handleCreate = data => {
console.log(data);
// this.state.information.push(data); X
const { information } = this.state;
this.setState({
information: information.concat(
// 합침
Object.assign({}, data, {
id: this.id++
}) // 새롭게 들어오는 데이터
)
});
};
이 부분을 말씀하시는 것 같은데,
먼저 Object.assign을 하는 이유는 information의 값을 업데이트하기 위함입니다.
다음으로 concat을 하는 이유는 연락처에 있는 이전의 데이터들에 이어 붙이기 위함입니다.
만약 information: Object.assign({}, data, {id: this.id++})) 만 있다면, information에는 새로운 name과 phone만 들어가게 됩니다.
추가적으로 이렇게도 가능합니다.
const { information } = this.state;
this.setState({
information: information.concat({
...data,
id: this.id++
})
});
답변 1