React Scan

0.5.3 · active · verified Sun Apr 19

React Scan is a performance monitoring tool for React applications designed to automatically detect re-render issues and potential performance bottlenecks. Unlike React's built-in `Profiler` or other tools like `Why Did You Render?`, `react-scan` aims to provide simple visual cues and requires minimal to no code changes for integration. It offers a portable API accessible via npm, CDN script tags, or a command-line interface, making it versatile for various development workflows across different React frameworks. The package is currently at version 0.5.3 and maintains an active development cycle with a moderate release cadence, though previous versions noted a period of slower releases. Key features include highlighting re-rendering components, providing notifications for performance issues, and offering a persistent, draggable toolbar for inspection.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate React Scan into a React application, enabling it conditionally for development environments. It shows the basic `scan()` function call with an `Options` object, and a simple React component structure that would trigger re-renders visible via the React Scan toolbar.

import { scan, Options } from 'react-scan';
import React, { useState, useEffect } from 'react';
import ReactDOM from 'react-dom/client';

// Define options for React Scan, typically enabling it only in development.
const scanOptions: Options = {
  enabled: process.env.NODE_ENV === 'development', // Crucial for conditional activation
  debug: false, // Set to true for verbose logging
  // You can add more options like filters or thresholds here.
};

// Initialize React Scan. It's best practice to call it once early in your app lifecycle.
// Ensure this runs only client-side if your environment is universal (SSR/SSG).
if (typeof window !== 'undefined') {
  scan(scanOptions);
}

function App() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    console.log('App component mounted or re-rendered');
  });

  return (
    <div>
      <h1>React Scan Basic Example</h1>
      <p>Current count: {count}</p>
      <button onClick={() => setCount(prev => prev + 1)}>
        Increment Count
      </button>
      <ChildComponent value={count} />
    </div>
  );
}

function ChildComponent({ value }: { value: number }) {
  // This component will re-render whenever its 'value' prop changes.
  // React Scan will highlight it and report its renders if enabled.
  return (
    <p>Child component showing value: {value}</p>
  );
}

// Render your application using ReactDOM.
const rootElement = document.getElementById('root');
if (rootElement) {
  const root = ReactDOM.createRoot(rootElement);
  root.render(
    <React.StrictMode>
      <App />
    </React.StrictMode>
  );
} else {
  console.error("Root element not found! Ensure an element with id='root' exists in your HTML.");
}

view raw JSON →