Skip to content

Conversation

djhi
Copy link
Collaborator

@djhi djhi commented Oct 3, 2025

Problem

After #10857, when passing parameters at declaration time to a mutation hook in optimistic or undoable mode, those params may be modified by the side effects before the actual mutation is called.

For instance, if you pass the selectedIds from the ListContext to a useUpdateMany hook and use the useUnselectAll hook to reset the selectedIds in the onSuccess side effect, then the actual mutation will be called with an empty array of ids.

You can see the issue like this:

Solution

Keep the params in an additional ref that is freezed once the mutate callback is called.

How To Test

  • make run
  • http://localhost:8080/#/posts
  • select multiple rows and click the Reset views button
  • check the dataProvider call in the console and ensure the correct ids were passed to updateMany

Additional Checks

  • The PR targets master for a bugfix or a documentation fix, or next for a feature
  • The PR includes unit tests (if not possible, describe why)

… may pass invalid parameters to the mutation

## Problem

After #10857, when passing parameters at declaration time to a mutation hook in `optimistic` or `undoable` mode, those params may be modified by the side effects before the actual mutation is called.

For instance, if you pass the `selectedIds` from the `ListContext` to a `useUpdateMany` hook and use the `useUnselectAll` hook to reset the `selectedIds` in the `onSuccess` side effect, then the actual mutation will be called with an empty array of ids.

## Solution

Keep the params in an additional ref that is freezed once the `mutate` callback is called.
@djhi djhi added the RFR Ready For Review label Oct 3, 2025
@djhi djhi added this to the 5.12.0 milestone Oct 3, 2025
@slax57 slax57 requested review from slax57 and Copilot October 6, 2025 11:03
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a bug where mutations using optimistic or undoable mode would receive stale parameters when side effects modify the parameters between the mutation call and execution. The fix introduces a new ref to preserve parameters at the exact moment the mutation is called.

  • Adds paramsAtExecutionTimeRef to freeze parameters when mutate is called
  • Updates all optimistic and undoable mutation paths to use the frozen parameters
  • Preserves existing behavior for pessimistic mutations and declaration-time parameter updates

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
packages/ra-core/src/dataProvider/useMutationWithMutationMode.ts Implements the core fix by adding parameter freezing logic and updating mutation execution paths
packages/ra-core/src/dataProvider/useMutationWithMutationMode.spec.tsx Adds test coverage to verify parameters are preserved correctly in optimistic mode

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@slax57 slax57 merged commit 468828c into next Oct 6, 2025
15 checks passed
@slax57 slax57 deleted the fix-use-mutation-with-mutation-modes branch October 6, 2025 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Development

Successfully merging this pull request may close these issues.

2 participants