Cannot set property 'state' of undefined in react functional component code example
Example 1: TypeError: Cannot read property 'setState' of undefined
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = {
count : 1
};
this.delta = this.delta.bind(this);
}
delta() {
this.setState({
count : this.state.count++
});
}
render() {
return (
<div>
<h1>{this.state.count}</h1>
<button onClick={this.delta}>+</button>
</div>
);
}
}
Example 2: Cannot read property 'setState' of undefined
<input onChange={this.onInputChange.bind(this)} placeholder="First Name" />
constructor(){
this.onInputChange = this.onInputChange.bind(this)
}