You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit a1335fa
Browse filesBrowse the repository at this point in the historyBrowse files
Ali Alakbarli
committed
perf: use React 18 startTransition to prevent spinner freeze and infinite re-render loops
Root Cause Analysis:
- React Query with superjson deserializes Date objects on each query result,
producing new object references even when data is identical
- This breaks structural sharing, causing useMemo dependencies to trigger
unnecessarily
- forceUpdate({}) synchronous calls blocked main thread, preventing CSS
animation paint commits (spinner freeze)
- Batch JSON.parse in useMemo blocked main thread for 50-200ms
Changes:
1. mock-api.ts - Convert useStabilizedRef to useStableKey pattern
- Returns stable string key instead of data reference
- Key only changes when serialized data actually differs
- Used as useMemo dependency while reading result.data directly inside
2. mock-api.ts - Add useStableKey to useSubChatMessagesBatch
- Previously missing stabilization caused infinite re-render cascade
- Stable key prevents unnecessary Map recreation
3. mock-api.ts - Wrap batch parsing in startTransition
- Heavy JSON.parse now runs as low-priority update
- React yields to paint commits, allowing spinners to animate smoothly
- Uses useTransition hook with effect-based state update
4. active-chat.tsx - Remove synchronous forceUpdate({}) calls
- Zustand's useShallow subscriptions already trigger re-renders
- Synchronous updates blocked animation frames
5. active-chat.tsx - Fix new chat infinite loop condition
- Original: messagesBySubChatIdRef.current.has(subChatId) returned
true even for empty arrays
- Fixed: check actual message content (lazyMessages.length > 0)
- Prevents eviction loop when both cached and lazy-load have empty messages
Result: Smooth spinner animations during loading, no infinite re-renders on
new chat creation, stable data references across renders.
0 commit comments