Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React JS TypeError – this is not a function

<select value={this.state.selectedValue} onChange={this.handleChange} size={5}>
  <option value="grapefruit">Grapefruit</option>
  <option value="lime">Lime</option>
  <option value="coconut">Coconut</option>
  <option value="mango">Mango</option>
</select>

changecountry(value){
 //some code here
}
handlechange = (event) => {
  let value = event.target.value
 setTimeout(function (){
  this.changecountry(value)
}
}, 300);

I am trying to call changecountry inside setTimeout function.
getting this.changecountry is not a function error
what is wrong with this code?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Update your setTimeout to

setTimeout(() => this.changecountry(value) , 300);

this context will change inside function(){} in setTimeout. Arrow Function
is lexical scoped, will take reference from where it was created.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading