class Counter extends React.Component {
constructor(props) {
super(props);
this.state = { counter: 0 };
}
handleClick = () => {
this.setState(({ counter }) => ({
counter: counter + 1
}));
};
simError = () => {
x / 2;
};
render() {
if (this.state.counter === 5) {
// Simulate a JS error x is not defined
// this will crash the component `this.simErro()`
//this.simError()
// but this will not `setTimeout(this.simError, 0);`
//setTimeout(this.simError, 0);
}
return <h1 onClick={this.handleClick}>{this.state.counter}</h1>;
}
}
if you uncomment setTimeout(this.simError, 0); the component will not crash but you'll see the error in the console. link to codepen