You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When handleChange is invoked after selecting checkbox, this is undefined and so this.setState throws error 'cannot call setState on undefined'. However when I bind this to handleChange in the onChange attribute it works. Any thoughts on why I have to do this:
According to some info from this blog post ^ the examples need to be updated for es6 classes.
A few examples are given in blog for getting the intended context however the post is several months old and so wondering if a preferred pattern has emerged since?
I personally like the class properties pattern but then babel-plugin-syntax-class-properties needs to be included. And if you're just learning React you may easily take the example for granted and not realize the importance of context when it comes to event handlers...
class Todo extends React.Component{
change = (ev) => this.setState({ text: ev.target.value });
}
https://github.com/kohei-takata/learnyoureact/blob/master/exercises/state/solution/views/index.jsx#L45
When
handleChange
is invoked after selecting checkbox,this
is undefined and sothis.setState
throws error 'cannot call setState on undefined'. However when I bindthis
tohandleChange
in theonChange
attribute it works. Any thoughts on why I have to do this:rest of my Todo component:
The text was updated successfully, but these errors were encountered: