LogRocket React Plugin
LogRocket React Plugin (`logrocket-react`) is an optional add-on package designed to integrate LogRocket's session replay and monitoring capabilities specifically with React applications. It allows developers to search for user sessions based on interactions with specific React components within their app. The package is currently at version 7.0.0, supporting React 19. It maintains a distinct versioning scheme tied directly to major React versions, requiring users to install a specific `logrocket-react` version corresponding to their React installation (e.g., `v7` for React 19, `v6` for React 18). This tight coupling dictates its release cadence, typically aligning with new React major releases. A key differentiator is its reliance on the `displayName` property of React components for reporting component names, which often requires specific build tooling or manual intervention.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'init')
cause `LogRocketReact` was not correctly imported, or `LogRocket` (core) was not imported/initialized before attempting to initialize `LogRocketReact`.fixEnsure both `LogRocket` from `logrocket` and `LogRocketReact` from `logrocket-react` are imported. Call `LogRocket.init()` before `LogRocketReact.init(LogRocket)`. -
React Hook 'useContext' cannot be called inside a callback.
cause Using a LogRocket-related hook (if any were exposed, or if custom hooks within a LogRocket context were mis-used) outside of a functional component or another custom hook.fixEnsure all React hooks are called only from functional components or other custom hooks. This is a general React rule, but misinterpreting LogRocket's integration points could lead to it. -
Warning: Accessing PropTypes via the main 'react' package is deprecated. Use the 'prop-types' package instead.
cause Using older versions of React or LogRocket-react that might still rely on `React.PropTypes` directly, particularly if your build environment is configured for an older React version with a newer logrocket-react version.fixUpgrade all related packages, especially `react` and `logrocket-react`, to their latest compatible versions. Ensure `prop-types` is used for explicit type checking if needed for custom components.
Warnings
- breaking Major versions of `logrocket-react` are tightly coupled to specific major versions of React. Installing the wrong version will lead to runtime errors or unexpected behavior.
- gotcha Component names are not reported to LogRocket unless the `displayName` property is explicitly set on your React components, or your build tooling (e.g., Babel plugins) automatically adds it.
- breaking The `init` function signature and export pattern have changed across major versions. Older versions might have used named exports like `setupLogRocketReact` or different parameters.
Install
-
npm install logrocket-react -
yarn add logrocket-react -
pnpm add logrocket-react
Imports
- LogRocketReact
const LogRocketReact = require('logrocket-react');import LogRocketReact from 'logrocket-react';
- setupLogRocketReact
import LogRocketReact from 'logrocket-react'; LogRocketReact.setup(LogRocket);
import { setupLogRocketReact } from 'logrocket-react'; - LogRocket
import LogRocket from 'logrocket';
Quickstart
import React from 'react';
import ReactDOM from 'react-dom/client';
import LogRocket from 'logrocket';
import LogRocketReact from 'logrocket-react';
// Initialize LogRocket core first
LogRocket.init(process.env.LOGROCKET_APP_ID ?? 'YOUR_APP_ID/YOUR_PROJECT_ID');
// Then initialize the React plugin
LogRocketReact.init(LogRocket);
interface ButtonProps {
onClick: () => void;
children: React.ReactNode;
}
// Ensure displayName is set for LogRocket to identify components
const MyButton = ({ onClick, children }: ButtonProps) => {
return (
<button onClick={onClick} style={{ padding: '10px 20px', cursor: 'pointer' }}>
{children}
</button>
);
};
MyButton.displayName = 'MyInteractiveButton'; // Critical for LogRocket component tracking
const App = () => {
const handleClick = () => {
console.log('Button clicked!');
LogRocket.track('UserClickedMyButton', { buttonName: 'MyInteractiveButton' });
};
return (
<div>
<h1>LogRocket React Integration</h1>
<p>This button interaction will be tracked by LogRocket.</p>
<MyButton onClick={handleClick}>Click Me</MyButton>
</div>
);
};
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);