LogRocket React Plugin

7.0.0 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize LogRocket and its React plugin, then render a simple React component that explicitly sets its `displayName` property, which is crucial for LogRocket's component tracking feature. It also shows how to manually track an event.

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

view raw JSON →