8000 Use stable keys for rows and columns in the fixed grid · Issue #1875 · cboard-org/cboard · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Use stable keys for rows and columns in the fixed grid #1875
Open
@RodriSanchez1

Description

@RodriSanchez1

Description
Currently, our grid component maps over rows and columns using array indexes (rowIndex and columnIndex) as React keys. When rows or columns are inserted, removed, or reordered, this causes React to remount entire <Row> and <DroppableCell> wrappers, even if the underlying <Tile> components have stable tile.id keys. This unnecessary remounting leads to lost component state, focus issues, and degraded performance.

Steps to Reproduce

  1. Render the grid with N rows and M columns.
  2. Each <Row> is keyed via key={rowIndex}.
  3. Each <DroppableCell> inside the row is keyed via key={columnIndex}.
  4. Change the data to insert or remove a row/column before existing ones.
  5. Observe that all affected rows/cells remount (losing state) instead of only the inserted/removed ones.

Expected Behavior

  • Only the newly inserted or removed rows/cells should mount or unmount.
  • Existing rows, cells, and <Tile> components should preserve their DOM, state, and focus when their position shifts.

Actual Behavior

  • React treats all rows/cells after the insertion/removal point as new, tearing down and recreating them.
  • Child <Tile> components unmount and remount, losing any local state or user focus.

Proposed Solution

  1. Row keys: Use a stable identifier for each row (e.g., row.id) instead of the rowIndex.

  2. Cell keys: Generate a unique key per cell that combines row and column context or uses item.id when present:

    const cellKey = item
      ? tile.id
      : `empty-${row.id}-${columnIndex}`;
    <DroppableCell key={cellKey} ...>
  3. Retain key={tile.id} on the <Tile> to preserve its identity across moves.

Implementing these changes will ensure React’s reconciliation only updates what truly changed, preserving component state and improving performance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0