Skip to content
This repository was archived by the owner on Sep 16, 2026. It is now read-only.

fix: React 19 virtualizer optimization workaround - #434

Merged
leoisadev1 merged 1 commit into
mainfrom
fix/react19-virtualizer-ref-workaround
Nov 27, 2025
Merged

leoisadev1 merged 1 commit into
mainfrom
fix/react19-virtualizer-ref-workaround

Conversation

@leoisadev1

Copy link
Copy Markdown
Member

Summary

Root Cause

React 19's compiler optimizations can eliminate virtualizer.getVirtualItems() calls when the return value appears unused (not stored in state). This causes the virtualizer to malfunction, leading to infinite re-render loops.

Solution

Based on TanStack/virtual issue #743, storing the virtualizer in a ref prevents React 19 from optimizing it away:

const virtualizerRef = useRef(virtualizer);
virtualizerRef.current = virtualizer;

// Use virtualizerRef.current.getVirtualItems() instead of virtualizer.getVirtualItems()

Test plan

🤖 Generated with Claude Code

React 19's compiler optimizations can eliminate virtualizer.getVirtualItems()
calls when the return value appears unused. This causes the virtualizer to
not render items correctly, leading to infinite re-render loops (error #185)
and hydration mismatches (error #418).

Fix: Store virtualizer in a ref and access through virtualizerRef.current
to prevent React 19 from optimizing away the virtualizer state updates.

Reference: TanStack/virtual#743

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@vercel

vercel Bot commented Nov 27, 2025

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
openchat-web Building Building Preview Comment Nov 27, 2025 0:12am

@leoisadev1
leoisadev1 merged commit eb56b39 into main Nov 27, 2025
@greptile-apps

greptile-apps Bot commented Nov 27, 2025

Copy link
Copy Markdown
Contributor

Greptile Overview

Greptile Summary

This PR implements a workaround for React 19 compiler optimizations that were causing the virtualizer to malfunction, leading to infinite re-render loops and hydration errors.

The main changes:

  • Stores the virtualizer instance in virtualizerRef to prevent React 19 from optimizing away state updates
  • Accesses getVirtualItems(), getTotalSize(), and measureElement() through virtualizerRef.current instead of directly
  • Removes virtualizer from measureElementRef dependency array, keeping only isMounted for stability

Critical Issue Found:

  • The useMemo dependency array on line 166 includes virtualizerRef.current.getTotalSize() which is a function call that returns a new value on every render, completely defeating the memoization. This should depend on state variables like messages.length that trigger size changes, not the computed result itself.

Confidence Score: 3/5

  • This PR addresses critical React 19 issues but introduces a logic bug in the useMemo dependency array that defeats the memoization optimization
  • The virtualizer ref workaround is a valid approach for React 19 (score +2), but the useMemo dependency array bug causes unnecessary re-renders on every render cycle (score -2). The fix itself is well-documented and addresses real issues fix: use headers() as fallback for cookie retrieval #418 and fix(web): improve accessibility #185.
  • Pay close attention to apps/web/src/components/chat-messages-panel.tsx line 159-167 where the useMemo dependency array needs correction

Important Files Changed

File Analysis

Filename Score Overview
apps/web/src/components/chat-messages-panel.tsx 3/5 Added React 19 virtualizer workaround by storing virtualizer in ref to prevent compiler optimizations. Fixed measureElement callback stability. One critical issue with useMemo dependency array defeats memoization.

Sequence Diagram

sequenceDiagram
    participant React as React 19 Compiler
    participant Component as ChatMessagesPanel
    participant Virtualizer as useVirtualizer
    participant Ref as virtualizerRef
    participant DOM as Virtual DOM

    Note over React,Component: Component Render Phase
    Component->>Virtualizer: useVirtualizer({ count, enabled })
    Virtualizer-->>Component: virtualizer instance
    
    Note over Component,Ref: React 19 Fix Applied
    Component->>Ref: virtualizerRef.current = virtualizer
    Note right of Ref: Store in ref to prevent<br/>compiler optimization
    
    Note over Component,DOM: Render Virtual Items
    Component->>Ref: virtualizerRef.current.getVirtualItems()
    Ref->>Virtualizer: getVirtualItems()
    Virtualizer-->>Ref: virtual items array
    Ref-->>Component: virtual items array
    
    Component->>Ref: virtualizerRef.current.getTotalSize()
    Ref->>Virtualizer: getTotalSize()
    Virtualizer-->>Ref: total size
    Ref-->>Component: total size
    
    Component->>DOM: Render virtualized messages
    
    Note over Component,Ref: Measurement Phase
    DOM->>Component: ref callback fires
    Component->>Ref: virtualizerRef.current.measureElement(node)
    Ref->>Virtualizer: measureElement(node)
    Note right of Virtualizer: Updates internal state<br/>triggers re-render
    
    Note over React,Virtualizer: Without Fix (Optimized Away)
    React->>React: Detects unused getVirtualItems()
    React->>React: Eliminates virtualizer calls
    Note right of React: Causes infinite loops<br/>and hydration errors
Loading

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment on lines 159 to 167
const virtualListContainerStyle = useMemo(
() => ({
height: `${virtualizer.getTotalSize()}px`,
height: `${virtualizerRef.current.getTotalSize()}px`,
width: "100%",
position: "relative" as const,
}),
[virtualizer]
// eslint-disable-next-line react-hooks/exhaustive-deps
[virtualizerRef.current.getTotalSize()]
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: useMemo dependency array includes function call result virtualizerRef.current.getTotalSize() which defeats memoization - causes recalculation on every render. Should depend on messages.length or similar state that triggers size changes.

Suggested change
const virtualListContainerStyle = useMemo(
() => ({
height: `${virtualizer.getTotalSize()}px`,
height: `${virtualizerRef.current.getTotalSize()}px`,
width: "100%",
position: "relative" as const,
}),
[virtualizer]
// eslint-disable-next-line react-hooks/exhaustive-deps
[virtualizerRef.current.getTotalSize()]
);
const virtualListContainerStyle = useMemo(
() => ({
height: `${virtualizerRef.current.getTotalSize()}px`,
width: "100%",
position: "relative" as const,
}),
[messages.length, shouldVirtualize]
);

leoisadev1 added a commit that referenced this pull request Nov 27, 2025
EMERGENCY FIX: The @tanstack/react-virtual library causes infinite re-render
loops during hydration in React 19, even with the virtualizerRef workaround
from PR #434. The error occurs INSIDE the library code, not in our wrapper.

This completely disables virtualization until @tanstack/react-virtual
officially supports React 19.

References:
- TanStack/virtual#743
- TanStack/virtual#924

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
leoisadev1 added a commit that referenced this pull request Nov 27, 2025
…435)

EMERGENCY FIX: The @tanstack/react-virtual library causes infinite re-render
loops during hydration in React 19, even with the virtualizerRef workaround
from PR #434. The error occurs INSIDE the library code, not in our wrapper.

This completely disables virtualization until @tanstack/react-virtual
officially supports React 19.

References:
- TanStack/virtual#743
- TanStack/virtual#924

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude <[email protected]>
@leoisadev1
leoisadev1 deleted the fix/react19-virtualizer-ref-workaround branch January 20, 2026 22:26

This branch was successfully deployed

1 active deployment
Preview — 6e6e1192 Deployed Nov 27, 2025 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant