I want to push element in array of state variable on each click of check box. The problem is when i click on checkbox the value get pushed into array but when i click another checkbox array washout old value and only one value is present in it.
export default class Demo extends Component{
constructor(props){
super(props);
this.state = {
attachmentList : []
};
}
addAttachmentid = (e) => {
console.log("1st",this.state.attachmentList)
if(e.target.checked){
this.setState({
attachmentList:this.state.attachmentList.push(e.target.value)
});
}
else{
this.setState({
attachmentList:this.state.attachmentList.pop(e.target.value)
});
}
console.log("attachmentlist",this.state.attachmentList)
}
render(){
return (
<input type="checkbox" value="20" onChange={this.addAttachmentid} />
<input type="checkbox" value="21" onChange={this.addAttachmentid} />
<input type="checkbox" value="22" onChange={this.addAttachmentid} />
)
}
}