What is @sentry/react?
The @sentry/react package is designed for error monitoring and performance tracking in React applications. It provides a comprehensive set of tools to capture exceptions, track performance issues, and improve the overall reliability of React applications. By integrating Sentry into a React project, developers can gain insights into errors and performance bottlenecks, allowing for quicker debugging and optimization.
What are @sentry/react's main functionalities?
Error Boundary
This feature automatically catches JavaScript errors anywhere in the child component tree of the Error Boundary and logs them to Sentry. It also displays a fallback UI instead of the component tree that crashed.
import React from 'react';
import * as Sentry from '@sentry/react';
const MyApp = () => (
<Sentry.ErrorBoundary fallback={'An error has occurred'}>
<YourApp />
</Sentry.ErrorBoundary>
);
Performance Monitoring
Enables performance monitoring in your React application. By setting the `tracesSampleRate` to a value between 0 and 1, you can control the percentage of transactions sent to Sentry for performance analysis.
import * as Sentry from '@sentry/react';
Sentry.init({
dsn: 'YOUR_DSN',
tracesSampleRate: 1.0,
});
Custom Error Tracking
Allows for manual reporting of errors or exceptions to Sentry, giving developers the flexibility to capture specific issues that may not be automatically detected.
import * as Sentry from '@sentry/react';
Sentry.captureException(new Error('This is my custom error.'));
Other packages similar to @sentry/react
logrocket
LogRocket is a frontend application monitoring solution that provides session replay, performance monitoring, and error tracking. While it offers similar error tracking capabilities to @sentry/react, LogRocket distinguishes itself with its session replay feature, allowing developers to see exactly what users saw when an error occurred.
bugsnag
Bugsnag offers error monitoring and application stability management for various programming languages and frameworks, including React. Compared to @sentry/react, Bugsnag emphasizes application stability metrics and provides detailed error grouping and analysis, making it easier to prioritize and fix critical errors.
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 occured</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);
8.29.0
Important Changes
- Beta releases of official Solid and SolidStart Sentry SDKs
This release marks the beta releases of the @sentry/solid
and @sentry/solidstart
Sentry SDKs. For details on how to
use them, check out the
Sentry Solid SDK README and the
Sentry SolidStart SDK README
respectively. Please reach out on GitHub if you have
any feedback or concerns.
- feat(node): Option to only wrap instrumented modules (#13139)
Adds the SDK option to only wrap ES modules with import-in-the-middle
that specifically need to be instrumented.
import * as Sentry from '@sentry/node';
Sentry.init({
dsn: '__PUBLIC_DSN__',
registerEsmLoaderHooks: { onlyIncludeInstrumentedModules: true },
});
- feat(node): Update OpenTelemetry packages to instrumentation v0.53.0 (#13587)
All internal OpenTelemetry instrumentation was updated to their latest version. This adds support for Mongoose v7 and v8
and fixes various bugs related to ESM mode.
Other Changes
- feat(nextjs): Emit warning when using turbopack (#13566)
- feat(nextjs): Future-proof Next.js config options overriding (#13586)
- feat(node): Add
generic-pool
integration (#13465) - feat(nuxt): Upload sourcemaps generated by Nitro (#13382)
- feat(solidstart): Add
browserTracingIntegration
by default (#13561) - feat(solidstart): Add
sentrySolidStartVite
plugin to simplify source maps upload (#13493) - feat(vue): Only start UI spans if parent is available (#13568)
- fix(cloudflare): Guard
context.waitUntil
call in request handler (#13549) - fix(gatsby): Fix assets path for sourcemaps upload (#13592)
- fix(nextjs): Use posix paths for sourcemap uploads (#13603)
- fix(node-fetch): Use stringified origin url (#13581)
- fix(node): Replace dashes in
generic-pool
span origins with underscores (#13579) - fix(replay): Fix types in WebVitalData (#13573)
- fix(replay): Improve replay web vital types (#13602)
- fix(utils): Keep logger on carrier (#13570)
Work in this release was contributed by @Zen-cronic. Thank you for your contribution!