0

I have introduced a basic error by accessing property length of null, in my component called App2. But my errorboundary is not triggering for the same. Here is the example live https://codepen.io/abhinavthinktank/pen/gEePPe. Im not sure why this behaviour is happening

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

  componentDidCatch() {
    // Display fallback UI
    this.setState({ hasError: true });
  }

  render() {
    const { hasError } = this.state;
    const { children } = this.props;
    if (hasError) {
      return <div>ERRRORRRRRRRRRRRRRRRRRRRRRRRRRRR</div>;
    }
    return children;
  }
}


class App extends React.Component {
  render() {
    let test2 = null;
    return (
      <div>
        {test2.length}
        <h1>Hello, React and ES6!</h1>
        <p>Let's play. :)</p>
      </div>
    );
  }
}

const App2 = props => {
  return(
  <ErrorBoundary>
    <App/>
    </ErrorBoundary>
    )
}

ReactDOM.render(<App2 />, document.getElementById("app"));
1

1 Answer 1

1

The problem is you are using react 15 but Error boundary is available from react 16 so it is not working. Here I have changed the version now it works fine. click here for codepen

// https://unpkg.com/react@16/umd/react.development.js
// https://unpkg.com/react-dom@16/umd/react-dom.development.js
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.