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.21.0
Important Changes
-
feat(browserProfiling): Add trace lifecycle mode for UI profiling (#17619)
Adds a new trace lifecycle mode for UI profiling, allowing profiles to be captured for the duration of a trace. A manual mode will be added in a future release.
-
feat(nuxt): Instrument Database (#17899)
Adds instrumentation for Nuxt database operations, enabling better performance tracking of database queries.
-
feat(nuxt): Instrument server cache API (#17886)
Adds instrumentation for Nuxt's server cache API, providing visibility into cache operations.
-
feat(nuxt): Instrument storage API (#17858)
Adds instrumentation for Nuxt's storage API, enabling tracking of storage operations.
Other Changes
- feat(browser): Add
onRequestSpanEnd hook to browser tracing integration (#17884)
- feat(nextjs): Support Next.js proxy files (#17926)
- feat(replay): Record outcome when event buffer size exceeded (#17946)
- fix(cloudflare): copy execution context in durable objects and handlers (#17786)
- fix(core): Fix and add missing cache attributes in Vercel AI (#17982)
- fix(core): Improve uuid performance (#17938)
- fix(ember): Use updated version for
clean-css (#17979)
- fix(nextjs): Don't set experimental instrumentation hook flag for next 16 (#17978)
- fix(nextjs): Inconsistent transaction naming for i18n routing (#17927)
- fix(nextjs): Update bundler detection (#17976)
<details>
<summary> <strong>Internal Changes</strong> </summary>
- build: Update to typescript 5.8.0 (#17710)
- chore: Add external contributor to CHANGELOG.md (#17949)
- chore(build): Upgrade nodemon to 3.1.10 (#17956)
- chore(ci): Fix external contributor action when multiple contributions existed (#17950)
- chore(solid): Remove unnecessary import from README (#17947)
- test(nextjs): Fix proxy/middleware test (#17970)
</details>
Work in this release was contributed by @0xbad0c0d3. Thank you for your contribution!