React Scan

repository·main·Indexed 12 days ago

https://github.com/aidenybai/react-scan

A performance monitoring tool that automatically detects and highlights unnecessary React component re-renders. It includes a visual UI for identifying bottlenecks, a CLI for scanning local or remote websites, a headless instrumentation package (react-scan/lite), and a Vite plugin (@react-scan/vite-plugin-react-scan) for seamless integration.

Tokens
18.1K
Snippets
62
Records
82
Agent score
96%

What's inside React Scan

  1. Install React Scan in React Router v7 via script tag

    main

    To use React Scan in a React Router v7 project via a CDN, add a <script> tag to your Layout component within app/root.

    Note: This method only works for React 19.

    Refer to the project's CDN Guide for available URLs.

    // app/root
    // ...
    export function Layout({ children }: { children: React.ReactNode }) {
      return (
        <html lang="en">
          <head>
            <script src="https://unpkg.com/react-scan/dist/auto.global.js" />
            <meta charSet="utf-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1" />
            <Meta />
            <Links />
          </head>
          <body>
            {children}
            <ScrollRestoration />
            <Scripts />
          </body>
        </html>
      );
    }
    // ...
  2. Install React Scan in Remix via script tag

    main

    You can add React Scan to your Remix application by adding a script tag to your <Layout> component in app/root.

    Important:

    • The script must run before any of your other scripts.
    • This method only works for React 19.
    • Refer to the CDN Guide for available URLs (e.g., https://unpkg.com/react-scan/dist/auto.global.js).
    // app/root.jsx
    import {
      Links,
      Meta,
      Scripts,
      ScrollRestoration,
    } from "@remix-run/react";
    
    export function Layout({ children }: { children: React.ReactNode }) {
      return (
        <html lang="en">
          <head>
            {/* Must run before any of your scripts */}
            <script src="https://unpkg.com/react-scan/dist/auto.global.js" />
            <meta charSet="utf-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1" />
            <Meta />
            <Links />
          </head>
          <body>
            {children}
            <ScrollRestoration />
            <Scripts />
          </body>
        </html>
      );
    }
  3. Initialize React Scan via CDN

    main

    To quickly initialize React Scan without a build step or package manager, you can include the auto.global.js script directly in your HTML. This script automatically initializes the scanner.

    <script src="https://cdn.jsdelivr.net/npm/react-scan/dist/auto.global.js"></script>
  4. Configure React Scan in Next.js (Pages Router)

    main

    In Next.js using the Pages Router, add the script inside your pages/_document.tsx within the Head component using next/script with the beforeInteractive strategy.

    import { Html, Head, Main, NextScript } from "next/document";
    import Script from "next/script";
    
    export default function Document() {
      return (
        <Html lang="en">
          <Head>
            <Script
              src="//unpkg.com/react-scan/dist/auto.global.js"
              crossOrigin="anonymous"
              strategy="beforeInteractive"
            />
          </Head>
          <body>
            <Main />
            <NextScript />
          </body>
        </Html>
      );
    }
  5. Install React Scan in React Router v7 via import

    main

    To use React Scan in a React Router v7 project via npm/import, follow these requirements:

    1. Import Order: You must import scan from react-scan before React, React DOM, or React Router. This is necessary because React Scan needs to hijack React DevTools before they are accessed by the renderers.
    2. Hydration: Call scan() inside a useEffect hook in your app/root component to ensure it only runs after hydration.
    3. Environments:
      • Use import { scan } from "react-scan" for default behavior (typically development).
      • Use import { scan } from "react-scan/all-environments" if you want React Scan to run in production as well.
    // app/root
    
    // Must be imported before React Router
    import { scan } from "react-scan"; 
    import { Links, Meta, Scripts, ScrollRestoration } from "react-router";
    import { useEffect } from "react";
    
    export function Layout({ children }) {
      useEffect(() => {
        // Make sure to run react-scan only after hydration
        scan({
          enabled: true,
        });
      }, []);
    
      return (
        <html lang="en">
          <head>
            <meta charSet="utf-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1" />
            <Meta />
            <Links />
          </head>
          <body>
            {children}
            <ScrollRestoration />
            <Scripts />
          </body>
        </html>
      );
    }
    // app/root
    
    // Must be imported before React Router
    import { scan } from "react-scan"; 
    import { Links, Meta, Scripts, ScrollRestoration } from "react-router";
    import { useEffect } from "react";
    
    export function Layout({ children }) {
      useEffect(() => {
        scan({
          enabled: true,
        });
      }, []);
    
      return (
        <html lang="en">
          <head>
            <meta charSet="utf-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1" />
            <Meta />
            <Links />
          </head>
          <body>
            {children}
            <ScrollRestoration />
            <Scripts />
          </body>
        </html>
      );
    }
  6. Install React Scan via script tag in Create React App

    main

    To use React Scan in a Create React App (CRA) project without using module imports, add a script tag to the <head> section of your index.html.

    Refer to the CDN Guide for the available URLs.

    <!doctype html>
    <html lang="en">
      <head>
        <script src="https://unpkg.com/react-scan/dist/auto.global.js"></script>
    
        <!-- rest of your scripts go under -->
      </head>
      <body>
        <!-- ... -->
      </body>
    </html>
  7. Initialize React Scan in Remix entry.client

    main

    Alternatively, you can initialize React Scan directly in your app/entry.client.jsx file. This is a valid approach for React 19 applications.

    Important: Call scan({ enabled: true }) before the hydration process begins.

    // app/entry.client.jsx
    import { RemixBrowser } from "@remix-run/react";
    import { StrictMode } from "react";
    import { hydrateRoot } from "react-dom/client";
    import { scan } from "react-scan";
    
    scan({
      enabled: true,
    });
    
    // Hydration must happen in sync!
    hydrateRoot(
      document,
      <StrictMode>
        <RemixBrowser />
      </StrictMode>
    );
  8. Install React Scan as a module import

    main

    You can manually initialize React Scan in your project entrypoint (e.g., src/main.jsx or src/index.jsx).

    CRITICAL: You must import scan from react-scan before importing React or any other React renderers (like react-dom). This is necessary because React Scan needs to hijack React DevTools before React initializes them.

    To enable scanning in production environments, use the react-scan/all-environments import path instead of the default react-scan path.

    // src/index
    import { scan } from "react-scan"; // must be imported before React and React DOM
    import React from "react";
    
    scan({
      enabled: true,
    });
  9. Enable React Scan in production for TanStack Start

    main

    To ensure react-scan runs in production environments, change your import statement to use the all-environments entry point.

    Standard import (development only): import { scan } from "react-scan";

    Production-ready import: import { scan } from "react-scan/all-environments";

    - import { scan } from "react-scan";
    + import { scan } from "react-scan/all-environments";
  10. Install React Scan in Next.js App Router via script tag

    main

    To use React Scan in a Next.js App Router project using a script tag, add the script to the <head> section of your app/layout.js (or .tsx) file. This method uses a global script from a CDN.

    Refer to the CDN Guide for the list of available URLs.

    // app/layout
    export default function RootLayout({ children }) {
      return (
        <html lang="en">
          <head>
            <script src="https://unpkg.com/react-scan/dist/auto.global.js" />
            {/* rest of your scripts go under */}
          </head>
          <body>{children}</body>
        </html>
      );
    }
  11. Configure React Scan via Script Tag (HTML/Vite)

    main

    For standard HTML or Vite projects, paste the following script tag into your index.html before any other scripts:

    <!-- paste this BEFORE any scripts -->
    <script
      crossOrigin="anonymous"
      src="//unpkg.com/react-scan/dist/auto.global.js"
    ></script>