Immutability Helper
배열이나 객체의 추가, 수정, 삭제를 더 쉽게 하기 위한 리액트 플러그인(라이브러리)
-설치
npm install --save react-addons-update@15.6.2
-설치 후, 적용하고자 하는 클래스에 import (update함수 사용)
import update from 'react-addons-update';
-아직 사용하지 않은 import는 어둡게 표시됨
-아래와 같이 사용한다
create(arr){//데이터 추가
this.setState({
arrData:update(this.state.arrData,{$push:[arr]})
})
}
edit(num,cnt){//데이터 수정
this.setState({
arrData:update(this.state.arrData,{
[this.state.selectedKey]:{
num:{$set:num},
cnt:{$set:cnt}
}
})
})
}
handleRemove(){//데이터 삭제
if(this.state.selectedKey < 0){
return;
}
this.setState({
arrData:update(this.state.arrData,
{$splice:[[this.state.selectedKey,1]]}),
selectedKey:-1
})
}
'React' 카테고리의 다른 글
[React]vscode에서 react파일 실행, react프로젝트 내보내기(node_modules) (0) | 2021.04.20 |
---|---|
[React]특징 및 설치순서, html파일 웹 브라우저에 띄우기 (0) | 2021.04.20 |