Skip to content

Mutation Optimistic Update Causes Reference Changes and Unnecessary Rerenders #8382

Open
@pavel-sindelka

Description

Describe the bug

When using a mutation for deleting an item from a list with an optimistic response, the implementation causes reference changes for all items following the deleted one. This leads to unnecessary re-renders of components that receive these items as props, even if they use React.memo.

The issue seems to stem from how the list is updated optimistically (filter function creates a new array, causing reference changes for all remaining items).

Your minimal, reproducible example

https://stackblitz.com/edit/sb1-tgynyr?file=components%2Ftodo-list.tsx

Steps to reproduce

  1. Open the provided StackBlitz example.
  2. Observe that TodoItem components rerender even when their data does not change (e.g., via console logs in React.memo-wrapped components).
  3. Delete a todo item using the delete button.
  4. Note that all TodoItem components after the deleted one re-render.

Expected behavior

When deleting a todo item and updating the list optimistically:

  • Only the removed item's reference should change.
  • Other items should retain their references to prevent unnecessary re-renders.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

any

Tanstack Query adapter

react-query

TanStack Query version

v5.62.0

TypeScript version

v5.2.2

Additional context

The issue is due to how the filter function creates a new array and changes references for all subsequent items. This behavior can negatively impact performance in scenarios with a large number of items or deeply nested component trees.

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