Understand.init(...).catchErrors() reports errors thrown in event handlers, timers and promises. Errors thrown while rendering are caught by React itself, so report them in one of the ways below.
React 19: root error callbacks
React 19 lets you handle every rendering error in one place when you create the root:
import { createRoot } from 'react-dom/client';
import Understand from '@understand/understand-js';
import App from './App';
Understand.init({ env: 'production', token: 'your-input-key' }).catchErrors();
const root = createRoot(document.getElementById('root'), {
// errors caught by an error boundary
onCaughtError: (error, errorInfo) => {
Understand.logError(error, { component_stack: errorInfo.componentStack });
},
// errors not caught by any error boundary
onUncaughtError: (error, errorInfo) => {
Understand.logError(error, { component_stack: errorInfo.componentStack });
}
});
root.render(<App />);
React 16 to 19: error boundaries
An error boundary shows a fallback UI and reports the error. Error boundaries must be class components:
import { Component } from 'react';
import Understand from '@understand/understand-js';
export class ErrorBoundary extends Component {
state = { hasError: false };
static getDerivedStateFromError() {
return { hasError: true };
}
componentDidCatch(error, info) {
Understand.logError(error, { component_stack: info.componentStack });
}
render() {
if (this.state.hasError) {
return this.props.fallback ?? <h1>Something went wrong.</h1>;
}
return this.props.children;
}
}
<ErrorBoundary fallback={<p>Could not load the widget.</p>}>
<MyWidget />
</ErrorBoundary>
If you use the react-error-boundary package, report from its onError prop instead:
import { ErrorBoundary } from 'react-error-boundary';
import Understand from '@understand/understand-js';
<ErrorBoundary
fallback={<p>Something went wrong.</p>}
onError={(error, info) => Understand.logError(error, { component_stack: info.componentStack })}
>
<MyWidget />
</ErrorBoundary>
Using Next.js (App Router, server components, edge)? See the Next.js guide.