0

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});
  }
  );
3
  • 3
    Post full component please Commented Jun 23, 2017 at 18:12
  • console.log(this) and you should know why? Commented Jun 23, 2017 at 18:14
  • You typically shouldn't use addEventListener with react. I'm guessing this is the search box element in this case. Also, this.state should be defined in the constructor. Commented Jun 23, 2017 at 18:18

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.