Official Sentry SDK for ReactJS
Links
General
This package is a wrapper around @sentry/browser, with added functionality related to React. All methods available in
@sentry/browser can be imported from @sentry/react.
To use this SDK, call Sentry.init(options) before you mount your React component.
import React from 'react';
import { createRoot } from 'react-dom/client';
import * as Sentry from '@sentry/react';
Sentry.init({
dsn: '__DSN__',
});
const container = document.getElementById(“app”);
const root = createRoot(container);
root.render(<App />);
React 19
Starting with React 19, the createRoot and hydrateRoot methods expose error hooks that can be used to capture errors
automatically. Use the Sentry.reactErrorHandler function to capture errors in the error hooks you are interested in.
const container = document.getElementById(“app”);
const root = createRoot(container, {
onUncaughtError: Sentry.reactErrorHandler((error, errorInfo) => {
console.warn('Uncaught error', error, errorInfo.componentStack);
}),
onCaughtError: Sentry.reactErrorHandler(),
onRecoverableError: Sentry.reactErrorHandler(),
});
root.render(<App />);
If you want more finely grained control over error handling, we recommend only adding the onUncaughtError and
onRecoverableError hooks and using an ErrorBoundary component instead of the onCaughtError hook.
ErrorBoundary
@sentry/react exports an ErrorBoundary component that will automatically send Javascript errors from inside a
component tree to Sentry, and set a fallback UI.
app.js
import React from 'react';
import * as Sentry from '@sentry/react';
function FallbackComponent() {
return <div>An error has occurred</div>;
}
class App extends React.Component {
render() {
return (
<Sentry.ErrorBoundary fallback={FallbackComponent} showDialog>
<OtherComponents />
</Sentry.ErrorBoundary>
);
}
}
export default App;
Profiler
@sentry/react exports a Profiler component that leverages the tracing features to add React-related spans to
transactions. If tracing is not enabled, the Profiler component will not work. The Profiler tracks component mount,
render duration and updates.
app.js
import React from 'react';
import * as Sentry from '@sentry/react';
class App extends React.Component {
render() {
return (
<FancyComponent>
<InsideComponent someProp={2} />
<AnotherComponent />
</FancyComponent>
);
}
}
export default Sentry.withProfiler(App);
10.20.0
Important Changes
-
feat(flags): Add Growthbook integration (#17440)
Adds a new Growthbook integration for feature flag support.
-
feat(solid): Add support for TanStack Router Solid (#17735)
Adds support for TanStack Router in the Solid SDK, enabling better routing instrumentation for Solid applications.
-
feat(nextjs): Support native debugIds in turbopack (#17853)
Adds support for native Debug IDs in Turbopack, improving source map resolution and error tracking for Next.js applications using Turbopack. Native Debug ID generation will be enabled automatically for compatible versions.
Other Changes
- feat(nextjs): Prepare for next 16 bundler default (#17868)
- feat(node): Capture
pino logger name (#17930)
- fix(browser): Ignore React 19.2+ component render measure entries (#17905)
- fix(nextjs): Fix createRouteManifest with basePath (#17838)
- fix(react): Add
POP guard for long-running pageload spans (#17867)
- fix(tracemetrics): Send boolean for internal replay attribute (#17908)
- ref(core): Add weight tracking logic to browser logs/metrics (#17901)
<details>
<summary> <strong>Internal Changes</strong> </summary>
- chore(nextjs): Add Next.js 16 peer dependency ([#17925](https://github.com/getsentry/sentry-javascript/pull/17925))
- chore(ci): Update Next.js canary testing ([#17939](https://github.com/getsentry/sentry-javascript/pull/17939))
- chore: Bump size limit ([#17941](https://github.com/getsentry/sentry-javascript/pull/17941))
- test(nextjs): Add next@16 e2e test ([#17922](https://github.com/getsentry/sentry-javascript/pull/17922))
- test(nextjs): Update next 15 tests ([#17919](https://github.com/getsentry/sentry-javascript/pull/17919))
- chore: Add external contributor to CHANGELOG.md ([#17915](https://github.com/getsentry/sentry-javascript/pull/17915))
- chore: Add external contributor to CHANGELOG.md ([#17928](https://github.com/getsentry/sentry-javascript/pull/17928))
- chore: Add external contributor to CHANGELOG.md ([#17940](https://github.com/getsentry/sentry-javascript/pull/17940))
</details>
Work in this release was contributed by @seoyeon9888, @madhuchavva and @thedanchez. Thank you for your contributions!