@vigilon/react
v0.2.25
Published
Vigilon error monitoring for React applications
Maintainers
Readme
@vigilon/react
Error monitoring for React applications. Everything in @vigilon/browser, plus what React needs: an error boundary, the React 19 root error handlers, and route templates from TanStack Router and React Router.
Install
npm install @vigilon/reactInstall only this package: it re-exports the @vigilon/browser API and pins the matching version. Requires React 18 or later; reporting uncaught and caught render errors through the root error handlers needs React 19.
Set up
In the Vigilon dashboard, open your project's Settings page, then the API Keys tab, and create a key of kind Browser. Under Allowed origins, you can list the origins your app is served from, one per line; https://*.example.com covers every subdomain, and an empty list accepts any origin. Requests from any other origin get HTTP 403. The key ships to every visitor's browser, so never use a Server key here.
Call init once, in a module of its own:
// instrument.ts
import { init } from "@vigilon/react";
init({
apiKey: "your-api-key",
appName: "dashboard",
environment: "production",
appVersion: "2026.10.31-1",
});Import it first in your entry point, above every other import, then pass the root error handlers to createRoot (or hydrateRoot):
// main.tsx
import "./instrument";
import { rootErrorHandlers } from "@vigilon/react";
import { createRoot } from "react-dom/client";
import { App } from "./App";
createRoot(document.getElementById("root")!, rootErrorHandlers()).render(<App />);Imports run before the importing module's own code, so an init call written in main.tsx itself would run only after App and everything it imports had loaded: errors thrown while those modules load would be missed, and a router tracked at module level would be tracked before init.
React 19 reports render errors through these root options rather than the window error event, so without them Vigilon does not see:
- errors that no error boundary catches (
react_uncaught), - errors an error boundary catches, including your router's (
react_error_boundary), - errors React recovers from, such as hydration mismatches (
react_recoverable). When React recovered by rendering again, it wraps the error in one of its own; the error your code threw is reported.
Each handler reports the error, then calls your own callback for that option if you pass one, or logs the error with console.error as React would:
createRoot(container, rootErrorHandlers({
onCaughtError: (error, errorInfo) => myLogger.warn(error, errorInfo.componentStack),
}));React 18 has only onRecoverableError, so wrap a React 18 app in an ErrorBoundary to report render errors.
Error boundary
ErrorBoundary reports errors thrown while rendering its children, with their component stack, and renders fallback in their place:
import { ErrorBoundary } from "@vigilon/react";
<ErrorBoundary
fallback={({ error, eventId, resetError }) => (
<div role="alert">
<p>Something went wrong.</p>
<button onClick={resetError}>Try again</button>
</div>
)}
>
<Invoices />
</ErrorBoundary>| Prop | |
|---|---|
| fallback | What to render after an error: a node, or a function that receives error, componentStack, eventId, and resetError. Defaults to nothing. |
| onError | Called with the error, its component stack, and the Vigilon event ID once the error is reported. |
| onReset | Called when resetError clears the boundary. |
An error is reported once even when both the boundary and the root handlers see it. The package is marked "use client", so the boundary works in frameworks that render on the server by default.
Pages
Vigilon groups an error's affected pages by route template, such as /users/:id. With a supported router, track it once after creating the router, and every error carries the template of the page it happened on:
// TanStack Router
import { trackTanStackRouter } from "@vigilon/react/tanstack-router";
const router = createRouter({ routeTree });
trackTanStackRouter(router);// React Router 6.4+ or 7, with a data router
import { trackReactRouter } from "@vigilon/react/react-router";
const router = createBrowserRouter(routes);
trackReactRouter(router);Track the router after init has run, which importing instrument.ts first in your entry point guarantees. Tracking sets the current page's template at once, and before init that is lost, so the first page's errors would carry a template derived from the URL instead.
Both return a function that stops tracking. With TanStack Router the template is set before the destination's loaders run, so a loader error carries the page it was loading.
React Router renders a route's errorElement for an error thrown by a loader or action without throwing it, so no React error handler sees it; trackReactRouter reports it instead, as an error boundary would. Of the error responses a loader or action throws, such as a Response or a data() error, only server errors (status 500 and above) are reported, by their status and without the response body; a 404 or other client error response, such as throw data("Not found", { status: 404 }), is expected control flow and is not. React Router apps that use <BrowserRouter> rather than a data router, and other routers, can call setRoute themselves on each navigation; without a template, Vigilon derives the page from the URL path.
Configuration and privacy
init takes the same options, and applies the same privacy rules, as @vigilon/browser.
Support
- Bugs and feature requests: GitHub Issues
- Security vulnerabilities: report them privately to [email protected]
