I'm new to React, and writing my first web application. I am trying to write a search method that looks through a list of doctors. I get an "Uncaught type error: this.setState is not a function". I tried to add .bind at the end of the function, still got the error. I'm not sure how to proceed. The code is below:
this.searchBox.addEventListener('keyup', function(text){
this.state = {
doctors: ["Dr Smith", "Dr Jones", "Dr Mary"],
//list of matched doctors
matchedDoctors: []
}
document.getElementById("demo").innerHTML = "hello world";
var list = [];
//console.log(this.state.doctors);
for(var i = 0; i<this.state.doctors.length; i++){
if(this.state.doctors[i].toLowerCase().includes(text.toString().toLowerCase())){
list.push(
this.state.doctors[i]
)
}
}
this.setState({matchedDoctors:list});
}
);
console.log(this)and you should know why?addEventListenerwith react. I'm guessingthisis the search box element in this case. Also,this.stateshould be defined in the constructor.