I get "TypeError: Cannot read property 'setState' of undefined" if I try to call setState in a function (edit or save). It works if I declare an arrow function instead, but I do not understand why?
import React, { Component } from 'react';
class Note extends Component {
constructor(props) {
super(props);
this.state = {
editing: false
}
}
edit (){
this.setState({ editing: true })
}
save() {
this.setState({ editing: false })
}
renderEditNote() {
return (
<div className="note">
<textarea></textarea>
<button onClick={this.save}>SAVE</button>
</div>
)
}
renderDisplayNote() {
return (
<div className="note">
<p>{this.props.children}</p>
<span>
<button onClick={this.edit}>Edit</button>
<button onClick={this.remove}>X</button>
</span>
</div>
)
}
render() {
return this.state.editing ? this.renderEditNote() : this.renderDisplayNote()
}
}
export default Note