This package provides React and React ecosystem integrations for Flashcat Browser RUM.
npm install @flashcatcloud/browser-rum @flashcatcloud/browser-rum-reactTo enable the React integration, pass the reactPlugin to the plugins option of the flashcatRum.init method:
import { flashcatRum } from '@flashcatcloud/browser-rum'
import { reactPlugin } from '@flashcatcloud/browser-rum-react'
flashcatRum.init({
applicationId: ...,
clientToken: ...,
...
plugins: [reactPlugin()],
})To track React component rendering errors, use one of the following:
- An
ErrorBoundarycomponent (see React documentation) that catches errors and reports them to Flashcat. - A function that you can use to report errors from your own
ErrorBoundarycomponent.
import { ErrorBoundary } from '@flashcatcloud/browser-rum-react'
function App() {
return (
<ErrorBoundary fallback={ErrorFallback}>
<MyComponent />
</ErrorBoundary>
)
}
function ErrorFallback({ resetError, error }: { resetError: () => void; error: unknown }) {
return (
<p>
Oops, something went wrong! <strong>{String(error)}</strong> <button onClick={resetError}>Retry</button>
</p>
)
}import { addReactError } from '@flashcatcloud/browser-rum-react'
class MyErrorBoundary extends React.Component {
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
addReactError(error, errorInfo)
}
render() {
...
}
}react-router v6 allows you to declare routes using the following methods:
- Create routers with
createMemoryRouter,createHashRouter, orcreateBrowserRouterfunctions. - Use the
useRouteshook. - Use the
Routescomponent.
To track route changes with the Flashcat RUM Browser SDK, first initialize the reactPlugin with the router: true option, then replace those functions with their equivalent from @flashcatcloud/browser-rum-react/react-router-v6. Example:
import { RouterProvider } from 'react-router-dom'
import { flashcatRum } from '@flashcatcloud/browser-rum'
import { reactPlugin } from '@flashcatcloud/browser-rum-react'
// Use "createBrowserRouter" from @flashcatcloud/browser-rum-react/react-router-v6 instead of react-router-dom:
import { createBrowserRouter } from '@flashcatcloud/browser-rum-react/react-router-v6'
flashcatRum.init({
...
plugins: [reactPlugin({ router: true })],
})
const router = createBrowserRouter([
{
path: '/',
element: <Root />,
...
},
])
ReactDOM.createRoot(document.getElementById('root')).render(<RouterProvider router={router} />)