Socket
Socket
Sign inDemoInstall

@sentry/react

Package Overview
Dependencies
5
Maintainers
11
Versions
355
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sentry/react


Version published
Maintainers
11
Created

Package description

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

Changelog

Source

8.10.0

Important Changes

  • feat(remix): Migrate to opentelemetry-instrumentation-remix. (#12110)

You can now simplify your remix instrumentation by opting-in like this:

const Sentry = require('@sentry/remix');

Sentry.init({
  dsn: YOUR_DSN
  // opt-in to new auto instrumentation
  autoInstrumentRemix: true,
});

With this setup, you do not need to add e.g. wrapExpressCreateRequestHandler anymore. Additionally, the quality of the captured data improves. The old way to use @sentry/remix continues to work, but it is encouraged to use the new setup.

Other Changes

  • feat(browser): Export thirdPartyErrorFilterIntegration from @sentry/browser (#12512)
  • feat(feedback): Allow passing tags field to any feedback config param (#12197)
  • feat(feedback): Improve screenshot quality for retina displays (#12487)
  • feat(feedback): Screenshots don't resize after cropping (#12481)
  • feat(node) add max lineno and colno limits (#12514)
  • feat(profiling) add global profile context while profiler is running (#12394)
  • feat(react): Add React version to events (#12390)
  • feat(replay): Add url to replay hydration error breadcrumb type (#12521)
  • fix(core): Ensure standalone spans respect sampled flag (#12533)
  • fix(core): Use maxValueLength in extra error data integration (#12174)
  • fix(feedback): Fix scrolling after feedback submission (#12499)
  • fix(feedback): Send feedback rejects invalid responses (#12518)
  • fix(nextjs): Update @rollup/plugin-commonjs (#12527)
  • fix(node): Ensure status is correct for http server span errors (#12477)
  • fix(node): UnifygetDynamicSamplingContextFromSpan (#12522)
  • fix(profiling): continuous profile chunks should be in seconds (#12532)
  • fix(remix): Add nativeFetch support for accessing request headers (#12479)
  • fix(remix): Export no-op as captureRemixServerException from client SDK (#12497)
  • ref(node) refactor contextlines to use readline (#12221)

Work in this release was contributed by @AndreyKovanov and @kiliman. Thank you for your contributions!

Readme

Source

Sentry

Official Sentry SDK for ReactJS

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 />);

// also works with hydrateRoot
// const domNode = document.getElementById('root');
// const root = hydrateRoot(domNode, reactNode);
// 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, {
  // Callback called when an error is thrown and not caught by an Error Boundary.
  onUncaughtError: Sentry.reactErrorHandler((error, errorInfo) => {
    console.warn('Uncaught error', error, errorInfo.componentStack);
  }),
  // Callback called when React catches an error in an Error Boundary.
  onCaughtError: Sentry.reactErrorHandler(),
  // Callback called when React automatically recovers from errors.
  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);

FAQs

Last updated on 19 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc