<div id="root">
</div>
class MyBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { error: false };
  }

  static getDerivedStateFromError(data) {
    return { error: true }; // update the state object
  }

  componentDidCatch(error, data) {
    // handle the error content here.
  }

  render() {
    const { error } = this.state;
    const { children } = this.props;

    if (error) return <p>Something wrong happen! 🧐</p>;

    return children;
  }
}

class DispacthError extends React.Component {
  componentDidMount = async () => {
    try {
      const response = await fetch('htt//fake.url'); // fake url to crash
    } catch(e) {
      throw new Error(e.toString()); // throwing a new error
    }
  }

  render() {
      <div>
         <p>hola</p>
      </div>
  }
}

const App = () => (
  <MyBoundary>
    <DispacthError />
  </MyBoundary>
)


ReactDOM.render(
  <App />,
  document.getElementById('root')
);
View Compiled

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://cdnjs.cloudflare.com/ajax/libs/react/16.13.0/umd/react.production.min.js
  2. https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.0/umd/react-dom.production.min.js