spread 연산 -React-

Gong Kang·2021년 9월 27일
0

spread 연산을 통해 배열을 쉽게 만들 수 있다.

배열 복사

habits: [
        {key: 1, name: "Reading", count: 0},
        {key: 2, name: "Running", count: 0},
        {key: 3, name: "Coding", count: 0}
    ]
    
const habits2 = [...this.state.habits]; -> 복사본

배열에 추가하여 새로운 배열 만들기

const habits3 = [...this.state.habits, {key: Date.now(), name: name, count:0}];
기존의 habits 배열을 그대로 가져오고 거기에 새롭게 추가하여 만든다
profile
꾸준히 하루에 한번씩..!

0개의 댓글