the problem seems simple, but I can't figure out how to solve it:
- Add-Button declared within the
render
-Function works just fine - Add-Button declared within the
state
itself doesn't work. As the code already mentions, it will throw the"TypeError: Cannot read property 'state' of undefined"
-Error
class App extends Component {
constructor() {
super();
this.state = {
someArrayOfObjects: [{
attr:
// SNIP ...
// Doesn't work!
// When clicked leads to this error: "TypeError: Cannot read property 'state' of undefined"
<button onClick={this.doAction}>Add</button>
// SNIP ...
}]
};
this.doAction = this.doAction.bind(this);
}
// SNIP ...
doAction() {
console.log(this.state);
}
render() {
return(
// SNIP...
// Works just fine
<button onClick={this.doAction}>Add</button>
)
}
}
What am I missing out?