React

by jonahe

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.production.min.js"></script>
<div id="app"></div>

CSS

body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}

li {
  margin: 8px 0;
}

h2 {
  font-weight: bold;
  margin-bottom: 15px;
}

.done {
  color: rgba(0, 0, 0, 0.3);
  text-decoration: line-through;
}

input {
  margin-right: 5px;
}

Babel + JSX

class FFErrorBoundary extends React.Component {
	constructor(props) {
    super(props);
    this.state = { hasError: false };
  }

  static getDerivedStateFromError(error) {
    // Update state so the next render will show the fallback UI.
    console.log("getDerivedStateFromError")
    return { hasError: true };
  }

  componentDidCatch(error, info) {
    // You can also log the error to an error reporting service
    console.log("componentDidCatch");
    //logErrorToMyService(error, info);
  }

  render() {
    if (this.state.hasError) {
      // You can render any custom fallback UI
      return <h1>Something went wrong.</h1>;
    }

    return this.props.children; 
  }
}

/* const withErrorBoundary = WrappedComponent => () => <FFErrorBoundary>
  <WrappedComponent /></FFErrorBoundary>;
*/

class TodoApp extends React.Component {
	componentDidMount() {
  	setTimeout(() => {
    	throw new Error("Wrong TODO!!!");
    } , 3000);
  }
  
  render() {
  	return <div>Tesst</div>;
  }
}

//const WrappedTodoApp = withErrorBoundary(TodoApp);


ReactDOM.render(<FFErrorBoundary><TodoApp /></FFErrorBoundary>, document.querySelector("#app"))