Skip to content

Add React.memo to ThreadListItemComponent to prevent unnecessary re-renders #1011

@MODSetter

Description

@MODSetter

Description

ThreadListItemComponent in thread-list.tsx renders inside a .map() for every chat thread. When the parent re-renders (e.g., switching active thread, toggling archive filter), all thread rows re-render even if their individual props haven't changed. Wrapping the component in React.memo lets React skip re-rendering rows whose props are referentially equal.

Vercel React Best Practices Rule: rerender-memo (5.5)

File to change

  • surfsense_web/components/assistant-ui/thread-list.tsx

What to do

  1. Add memo to the React import:
import { memo } from "react";
  1. Convert ThreadListItemComponent (lines 218-275) from a plain function to a memoized component:
// Before
function ThreadListItemComponent({
  thread, isActive, isArchived, onClick, onArchive, onUnarchive, onDelete,
}: ThreadListItemComponentProps) {
  return ( ... );
}

// After
const ThreadListItemComponent = memo(function ThreadListItemComponent({
  thread, isActive, isArchived, onClick, onArchive, onUnarchive, onDelete,
}: ThreadListItemComponentProps) {
  return ( ... );
});

No other changes needed to the component internals.

Note: For memo to be fully effective, the callbacks passed to each row should also be stable. Currently (lines 195-198), inline arrow functions create new references each render. That's a potential follow-up, but even without it, memo helps when thread and isActive are unchanged (the most common case).

Acceptance criteria

  • ThreadListItemComponent is wrapped in React.memo
  • Thread list still works: clicking, archiving, unarchiving, deleting all function correctly
  • In React DevTools Profiler, unchanged thread rows show as "Did not render" when another row is clicked

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions