1

I never understood how that this works if it ever works in my react I wrote <input onChange={()=>console.log(this.value)} /> Why doesn't it recognize this??? it tells me that it Cannot read property 'value' of undefined onChange anyone help please :/

1 Answers1

2

React doesn't bind the event listener with the element.
So, the this refers to the class Component in which its defined.
If it was in functional Component then this it undefined

So if your trying to read input elements value use this,

<input onChange={e => console.log(e.target.value)} />

Read More, Function And Class Components

ezio4df
  • 3,541
  • 6
  • 16
  • 31