Reputation: 27
First click on item console log item inside arr. Second click returns error uncaught type error includes is not a function. And console log arr is 1.
Chrome browser
constructor(props){
super(props)
this.state = {
arr = []
}
this.handleClick = this.handleClick.bind(this);
}
handleClick(e) {
let arr = this.state.arr
let item = e.target.id
if (!arr.includes(item)) {
this.setState({arr: arr.push(item)})
} else {
this.setState({arr: arr.filter(x => x !== item)})
}
}
Upvotes: 1
Views: 1977
Reputation: 344
react says that data is inmutable, also your handle click by far as I see is not binded, thus do this
handleClick = (e)=> {
let arr = this.state.arr
}
then make a copy of state
handleClick = (e)=> {
let arr = Object.assign([],this.state.arr)
}
and at the end set state of data
handleClick = (e)=> {
.....
this.setState({arr: newArr})
//{arr: arr}
}
Upvotes: 1