When I run the following code from chapter 7
class LightningCounter extends React.Component {
constructor(props) {
super(props);
this.state = {
strikes: 0,
};
}
timerTick() {
console.log(this)
this.setState({
strikes: this.state.strikes + 100,
});
}
componentDidMount() {
setInterval(this.timerTick, 2000);
}
render() {
return <h1>{this.state.strikes}</h1>;
}
}
There happens an error, because this in setInterval becomes window context, however it it just runs the function without setInterval it works correctly.