Skip to content

Instantly share code, notes, and snippets.

@mertkahyaoglu
Created October 2, 2017 18:42
Show Gist options
  • Save mertkahyaoglu/a28f2ade37d72f747529bbdc537c0ba2 to your computer and use it in GitHub Desktop.
Save mertkahyaoglu/a28f2ade37d72f747529bbdc537c0ba2 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import { render } from "react-dom";
class App extends Component {
constructor(props) {
super(props);
this.state = { error: null };
}
componentDidCatch(error, errorInfo) {
this.setState({error: error})
}
render() {
if (this.state.error) {
return "Malesef 5'e kadar sayabiliyorum.";
} else {
return <BuggyCounter />;
}
}
}
class BuggyCounter extends Component {
constructor(props) {
super(props);
this.state = { counter: 0 };
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.setState(({counter}) => ({
counter: counter + 1
}));
}
render() {
if (this.state.counter === 5) {
throw new Error('Sinsi bir hata!');
}
return [
<h1>{this.state.counter}</h1>,
<button onClick={this.handleClick}>Arttır!</button>
];
}
}
render(<App />, document.getElementById("root"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment