top of page
Logo der Online Agentur mdwp


`componentDidCatch` is a lifecycle method in React JS that catches JavaScript errors anywhere in a component's child tree. Instead of crashing the whole app, it gives you a chance to handle errors gracefully and display a fallback UI.

This lifecycle method is called during the "commit" phase, which allows side effects. It's like a JavaScript catch {} block, but for components.

Here is its syntax:
componentDidCatch(error, info)
- `error` is the error that was thrown.
- `info` is an object with `componentStack` key. The property `componentStack` is a string showing component stack trace.

A simple use of componentDidCatch could look like this:
class ErrorBoundary extends React.Component {
constructor(props) {
this.state = { hasError: false };

componentDidCatch(error, info) {
// Display fallback UI
this.setState({ hasError: true });
// You can also log the error to an error reporting service
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;
In this code, if a child component throws an error, the `componentDidCatch` lifecycle method gets triggered and sets the state `hasError` to true, causing the component to re-render and display a fallback UI ("Something went wrong.") instead of the actual UI that has caused the error. It can also be used to log error info to an error reporting service.

bottom of page