react-window

repository·main·Indexed 12 days ago

https://github.com/bvaughn/react-window

A React component library for high-performance rendering of large lists and grids using virtualization. Version 2.3.0 provides List and Grid components to prevent performance degradation when dealing with massive datasets, featuring support for dynamic sizes, overscanning, and an imperative API for programmatic scrolling.

Tokens
8.2K
Snippets
27
Records
40
Agent score
96%

What's inside react-window

  1. Configure SSR and HTML Streaming in Vike

    main

    Vike provides built-in support for Server-Side Rendering (SSR) and HTML Streaming:

    • SSR: Enabled by default. It can be disabled globally or for specific pages.
    • HTML Streaming: Can be enabled or disabled globally or for specific pages to optimize how content is sent to the browser.
  2. Understand Vike's + files interface

    main

    Vike uses a convention of + prefixed files to define the interface between the Vike framework and your application code. These files control configuration, data fetching, and component rendering.

    Key + files include:

    • +config.ts: Defines application settings (e.g., <title>).
    • +Page.tsx: The main component for a page.
    • +data.ts: Logic for fetching data required by the +Page.tsx component.
    • +Layout.tsx: A component that wraps your +Page.tsx components.
    • +Head.tsx: Used to set <head> tags.
    • +onPageTransitionStart.ts and +onPageTransitionEnd.ts: Hooks for managing page transition animations.
    • /pages/_error/+Page.tsx: The specialized component rendered when an error occurs.
  3. Configure Vike routing options

    main

    Vike provides three built-in routing mechanisms to determine how URLs map to your application components:

    1. Filesystem Routing: The URL of a page is determined by the location of its +Page.tsx file within the filesystem.
    2. Route Strings: Explicitly defining routes using string patterns.
    3. Route Functions: Using logic/functions to determine routing.
  4. Enable type-aware ESLint rules in Vite

    main

    For production applications, it is recommended to enable type-aware lint rules by updating your ESLint configuration. Replace tseslint.configs.recommended with tseslint.configs.recommendedTypeChecked, tseslint.configs.strictTypeChecked, or tseslint.configs.stylisticTypeChecked. You must also configure parserOptions to point to your tsconfig files.

    export default tseslint.config({
      extends: [
        // Remove ...tseslint.configs.recommended and replace with this
        ...tseslint.configs.recommendedTypeChecked,
        // Alternatively, use this for stricter rules
        ...tseslint.configs.strictTypeChecked,
        // Optionally, add this for stylistic rules
        ...tseslint.configs.stylisticTypeChecked
      ],
      languageOptions: {
        // other options...
        parserOptions: {
          project: ["./tsconfig.node.json", "./tsconfig.app.json"],
          tsconfigRootDir: import.meta.dirname
        }
      }
    });
  5. Add React-specific lint rules to ESLint

    main

    To add React-specific linting, install eslint-plugin-react-x and eslint-plugin-react-dom. Register them in the plugins object of your eslint.config.js and spread their recommended rules into the rules object.

    // eslint.config.js
    import reactX from "eslint-plugin-react-x";
    import reactDom from "eslint-plugin-react-dom";
    
    export default tseslint.config({
      plugins: {
        // Add the react-x and react-dom plugins
        "react-x": reactX,
        "react-dom": reactDom
      },
      rules: {
        // other rules...
        // Enable its recommended typescript rules
        ...reactX.configs["recommended-typescript"].rules,
        ...reactDom.configs.recommended.rules
      }
    });
  6. Provide custom keys with columnKey and rowKey

    main

    By default, react-window uses the index as the key. For better performance and UX in sortable or filterable grids (especially with stateful cell components), you can provide custom keys using columnKey and rowKey.

    Important: These functions are called during render and cannot be auto-memoized. You must wrap them in useCallback to avoid performance degradation.

    const columnKey = useCallback(({ columnIndex, data, rowIndex }: any) => {
      return `col-${columnIndex}-${rowIndex}`;
    }, []);
    
    <Grid
      columnKey={columnKey}
      {/* ... */}
    />
  7. Use the Grid component for large 2D datasets

    main

    The Grid component renders large datasets organized into rows and columns using virtualization. Unlike the List component, Grid requires that cell sizes (row height and column width) are known ahead of time or can be derived from data without rendering. It uses absolute positioning to place cells within a scrollable container.

    import { Grid } from 'react-window';
    
    // Example usage concept
    <Grid
      columnCount={100}
      rowCount={100}
      columnWidth={150}
      rowHeight={50}
      cellComponent={MyCellComponent}
      cellProps={{ someData: data }}
    />
  8. Configure the List component

    main

    The List component renders data with many rows. It requires a component to render each row, the total number of rows, and the height of each row.

    Required Props

    • rowComponent: A React component responsible for rendering a row. It receives index and style props by default, plus any props passed via rowProps. The prop types are exported as RowComponentProps.
    • rowCount: Total number of items to be rendered.
    • rowHeight: The height of each row. Supported formats:
      • number (pixels)
      • string (percentage of the grid's current height)
      • function (returns pixel height given an index and cellProps)
      • A dynamic row height cache from the useDynamicRowHeight hook.
    • rowProps: Additional props passed to the rowComponent. The List re-renders rows when this object changes. Warning: Do not include ariaAttributes, index, or style in this object.

    Key Optional Props

    • rowKey: Custom key for rows. Use this for better UX in sortable/filterable lists. Important: Always wrap this in useCallback; do not use an inline function.
    • listRef: Ref to interact with the imperative API (scrolling and getting the outermost DOM element). Use useListRef or useListCallbackRef in TypeScript.
    • overscanCount: Number of additional rows to render outside the visible area to reduce flickering.
    • onRowsRendered: Callback triggered when the range of visible rows changes.