-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
docs(js): React Quick Start guide for Error Monitoring feature only #13061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a443103
React Quick Start guide for Error Monitoring feature only
inventarSarah cef877a
Merge branch 'master' of github.com:getsentry/sentry-docs into smi/qu…
inventarSarah 310a83c
update section on error boundary
inventarSarah a0ec11d
make Step 4 optional
inventarSarah bb02c35
Merge branch 'master' of github.com:getsentry/sentry-docs into smi/qu…
inventarSarah b2939c2
Update JS snippet
inventarSarah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,40 @@ | ||
| --- | ||
| title: React | ||
| description: "Learn about Sentry's React SDK and how it automatically reports errors and exceptions in your application." | ||
| description: "Learn how to manually set up Sentry in your React app and capture your first errors." | ||
| sdk: sentry.javascript.react | ||
| categories: | ||
| - javascript | ||
| - browser | ||
| --- | ||
|
|
||
| <Alert> | ||
|
|
||
| Sentry's React SDK enables automatic reporting of errors and exceptions. The SDK is a wrapper around @sentry/browser, with added functionality related to React. All methods available in @sentry/browser can be imported from @sentry/react. | ||
|
|
||
| </Alert> | ||
|
|
||
| <PlatformContent includePath="getting-started-prerequisites" /> | ||
|
|
||
| ## Features | ||
|
|
||
| In addition to capturing errors, you can monitor interactions between multiple services or applications by [enabling tracing](/concepts/key-terms/tracing/). You can also get to the root of an error or performance issue faster, by watching a video-like reproduction of a user session with [session replay](/product/explore/session-replay/web/getting-started/). | ||
| ## Step 1: Install | ||
|
|
||
| Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below. | ||
|
|
||
| ## Install | ||
| Choose the features you want to configure, and this guide will show you how: | ||
|
|
||
| <OnboardingOptionButtons | ||
| options={["error-monitoring", "performance", "session-replay"]} | ||
| /> | ||
|
|
||
| Sentry captures data by using an SDK within your application’s runtime. | ||
| <Expandable title="Want to learn more about these features?"> | ||
|
|
||
| - [**Issues**](/product/issues) (always enabled): Sentry's core error monitoring product that automatically reports errors, | ||
| uncaught exceptions, and unhandled rejections. If you have something that | ||
| looks like an exception, Sentry can capture it. | ||
| - [**Tracing**](/product/tracing): Track software performance while seeing the | ||
| impact of errors across multiple systems. For example, distributed tracing | ||
| allows you to follow a request from the frontend to the backend and back. | ||
| - [**Session Replay**](/product/explore/session-replay/web): | ||
| Get to the root cause of an issue faster by viewing a video-like reproduction | ||
| of what was happening in the user's browser before, during, and after the | ||
| problem. | ||
|
|
||
| </Expandable> | ||
|
|
||
| ### Install the Sentry SDK | ||
|
|
||
| Run the command for your preferred package manager to add the Sentry SDK to your application: | ||
|
|
||
| ```bash {tabTitle:npm} | ||
| npm install @sentry/react --save | ||
|
|
@@ -41,11 +48,13 @@ yarn add @sentry/react | |
| pnpm add @sentry/react | ||
| ``` | ||
|
|
||
| ## Configure | ||
| ## Step 2: Configure | ||
|
|
||
| Sentry supports multiple versions of React Router. To learn how to configure them, read the <PlatformLink to="/configuration/integrations/react-router/">React Router Integration</PlatformLink> docs. | ||
|
|
||
| Sentry should be initialized as early as possible in your application. We recommend putting the Sentry initialization code into its own file and including that file as the first import in your application entry point as shown in the example below: | ||
| ### Initialize the Sentry SDK | ||
|
|
||
| To import and initialize Sentry, create a file in your project's root directory, for example, `instrument.js`, and add the following code: | ||
|
|
||
| ```javascript {filename:instrument.js} {"onboardingOptions": {"performance": "3-8, 13-21, 24-30", "session-replay": "22, 33-39"}} | ||
inventarSarah marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import { useEffect } from "react"; | ||
|
|
@@ -90,7 +99,9 @@ Sentry.init({ | |
| }); | ||
| ``` | ||
|
|
||
| Include the Sentry initialization file as the first import statement: | ||
| ### Apply Instrumentation to Your App | ||
|
|
||
| Initialize Sentry as early as possible in your application. We recommend putting the import of your initialization code as the first import in your app's entry point: | ||
|
|
||
| ```javascript | ||
| // Sentry initialization should be imported first! | ||
|
|
@@ -103,11 +114,14 @@ const root = createRoot(container); | |
| root.render(<App />); | ||
| ``` | ||
|
|
||
| Once this is done, all unhandled exceptions will be automatically captured by Sentry. | ||
| ## Step 3: Capture React Errors | ||
|
|
||
| To make sure Sentry captures all your app's errors, configure error handling based on your React version. | ||
|
|
||
| ### React 19 Error Reporting | ||
| ### Configure Error Hooks (React 19+) | ||
|
|
||
| Starting with React 19, the `createRoot` and `hydrateRoot` methods from `react-dom` will expose error hooks that are used to capture errors automatically. To customize how errors are handled in specific error hooks, use the `Sentry.reactErrorHandler` function. | ||
| The `createRoot` and `hydrateRoot` methods provide error hooks to capture errors automatically. These hooks apply to all React components mounted to the root container. | ||
| Integrate Sentry with these hooks and customize error handling: | ||
|
|
||
| ```javascript | ||
| import { createRoot } from "react-dom/client"; | ||
|
|
@@ -126,25 +140,61 @@ const root = createRoot(container, { | |
| root.render(); | ||
| ``` | ||
|
|
||
| These hooks apply to all React components that are mounted to the container which is passed onto `createRoot`/`hydrateRoot`. For more precise control over error handling, we recommend adding an `ErrorBoundary` component to your application. | ||
| ### Add Error Boundary Components (React \<19) | ||
|
|
||
| ### Add Error Boundary | ||
| Use the [`ErrorBoundary`](features/error-boundary/) component to automatically send errors from specific component trees to Sentry and provide a fallback UI: | ||
|
|
||
| If you're using React 16 or above, you can use the [Error Boundary](features/error-boundary/) component to automatically send JavaScript errors from inside a component tree to Sentry, and set a fallback UI. | ||
| ```javascript | ||
| import React from "react"; | ||
| import * as Sentry from "@sentry/react"; | ||
|
|
||
| ### Set Up React Router | ||
| <Sentry.ErrorBoundary fallback={<p>An error has occurred</p>}> | ||
| <Example /> | ||
| </Sentry.ErrorBoundary>; | ||
| ``` | ||
|
|
||
| <Alert level="info"> | ||
| To capture errors manually with your own error boundary, use the | ||
| `captureReactException` function as described | ||
| [here](features/error-boundary/#manually-capturing-errors). | ||
| </Alert> | ||
|
|
||
| <OnboardingOption optionId="performance"> | ||
inventarSarah marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ## Step 4: Set Up React Router | ||
|
||
|
|
||
| The React Router integration is designed to work with our tracing package. Learn more about set up for our [React Router Integration](configuration/integrations/react-router/). | ||
|
|
||
| ### Apply Redux | ||
| ## Step 5: Capture Redux State Data (Optional) | ||
|
|
||
| </OnboardingOption> | ||
|
|
||
| <OnboardingOption optionId="performance" hideForThisOption> | ||
| ## Step 4: Capture Redux State Data (Optional) | ||
| </OnboardingOption> | ||
|
|
||
| To apply Sentry to Redux, learn more about the [Redux Integration](configuration/integrations/redux/) and its options. | ||
| To capture Redux state data, use `Sentry.createReduxEnhancer` when initializing your Redux store. | ||
|
|
||
| <PlatformContent includePath="getting-started-sourcemaps" /> | ||
| <PlatformContent includePath="configuration/redux-init" /> | ||
|
|
||
| ## Verify | ||
| <OnboardingOption optionId="performance"> | ||
| ## Step 6: Add Readable Stack Traces With Source Maps (Optional) | ||
| </OnboardingOption> | ||
| <OnboardingOption optionId="performance" hideForThisOption> | ||
| ## Step 5: Add Readable Stack Traces With Source Maps (Optional) | ||
| </OnboardingOption> | ||
|
|
||
| This snippet includes an intentional error, so you can test that everything is working as soon as you set it up. | ||
| <PlatformContent includePath="getting-started-sourcemaps-short-version" /> | ||
|
|
||
| <OnboardingOption optionId="performance"> | ||
| ## Step 7: Verify Your Setup | ||
| </OnboardingOption> | ||
| <OnboardingOption optionId="performance" hideForThisOption> | ||
| ## Step 6: Verify Your Setup | ||
| </OnboardingOption> | ||
|
|
||
| Let's test your setup and confirm that Sentry is working correctly and sending data to your Sentry project. | ||
|
|
||
| Add the following test button to one of your pages, which will trigger an error that Sentry will capture when you click it: | ||
|
|
||
| ```javascript | ||
| <button | ||
|
|
@@ -157,10 +207,30 @@ This snippet includes an intentional error, so you can test that everything is w | |
| </button> | ||
| ``` | ||
|
|
||
| <Alert> | ||
| Open the page in a browser (for most React applications, this will be at localhost) and click the button to trigger a frontend error. | ||
|
|
||
| Learn more about manually capturing an error or message in our <PlatformLink to="/usage/">Usage documentation</PlatformLink>. | ||
| <PlatformContent includePath="getting-started-browser-sandbox-warning" /> | ||
|
|
||
| </Alert> | ||
| ### View Captured Data in Sentry | ||
|
|
||
| Now, head over to your project on [Sentry.io](https://sentry.io/) to view the collected data (it takes a couple of moments for the data to appear). | ||
|
|
||
| <PlatformContent includePath="getting-started-verify-locate-data" /> | ||
|
|
||
| ## Next Steps | ||
|
|
||
| At this point, you should have integrated Sentry into your React application and should already be sending data to your Sentry project. | ||
|
|
||
| Now's a good time to customize your setup and look into more advanced topics. Our next recommended steps for you are: | ||
|
|
||
| - Extend Sentry to your backend using one of our [SDKs](/) | ||
| - Continue to <PlatformLink to="/configuration">customize your configuration</PlatformLink> | ||
| - Make use of <PlatformLink to="/features/redux">React-specific features</PlatformLink> | ||
| - Learn how to <PlatformLink to="/usage">manually capture errors</PlatformLink> | ||
| - Avoid ad-blockers with <PlatformLink to="/troubleshooting/#using-the-tunnel-option">tunneling</PlatformLink> | ||
|
|
||
| <Expandable permalink={false} title="Are you having problems setting up the SDK?"> | ||
|
|
||
| - [Get support](https://sentry.zendesk.com/hc/en-us/) | ||
|
|
||
| To view and resolve the recorded error, log into [sentry.io](https://sentry.io) and select your project. Clicking on the error's title will open a page where you can see detailed information and mark it as resolved. | ||
| </Expandable> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
platform-includes/getting-started-browser-sandbox-warning/javascript.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <Alert level="warning" title="Important"> | ||
|
|
||
| Errors triggered from within your browser's developer tools (like the browser console) are sandboxed, so they will not trigger Sentry's error monitoring. | ||
|
|
||
| </Alert> |
5 changes: 5 additions & 0 deletions
5
platform-includes/getting-started-sourcemaps-short-version/javascript.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| The stack traces in your Sentry errors probably won't look like your actual code. To fix this, upload your source maps to Sentry. The easiest way to do this is by using the Sentry Wizard: | ||
|
|
||
| ```bash | ||
| npx @sentry/wizard@latest -i sourcemaps | ||
| ``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.