Skip to content

Conversation

djhi
Copy link
Collaborator

@djhi djhi commented Jul 28, 2025

Problem

The mutation hooks currently put the mutationMode and params they receive at declaration time in a ref but never update them from their props.

Solution

Add effects that sync the refs with the props.

How To Test

  • stories
  • tests

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)
  • The PR includes one or several stories (if not possible, describe why)
  • The documentation is up to date

Also, please make sure to read the contributing guidelines.

Copy link
Member

@fzaninotto fzaninotto left a comment

Choose a reason for hiding this comment

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

1% code, 99% stories and tests, we're good ;)

];
const dataProvider = {
getList: (resource, params) => {
console.log('getList', resource, params);
Copy link
Member

Choose a reason for hiding this comment

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

you should not leave the console.log here (same in delete and in other hook tests)

];
const dataProvider = {
getList: (resource, params) => {
console.log('getList', resource, params);
Copy link
Member

Choose a reason for hiding this comment

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

I think you should remove these logs and the console.log mocking on the test. These may have been useful during development, but now the story is explicit enough (and testable enough) not to rely on logs.
Besides, the test aren't reverting the mock on console.log.

];
const defaultDataProvider = {
getList: (resource, params) => {
console.log('getList', resource, params);
Copy link
Member

Choose a reason for hiding this comment

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

same, console.log is useless in tests

@fzaninotto fzaninotto merged commit 68d4fb8 into next Aug 29, 2025
16 checks passed
@fzaninotto fzaninotto deleted the use-mutation-refs branch August 29, 2025 13:24
@fzaninotto fzaninotto added this to the 5.11.0 milestone Aug 29, 2025
djhi added a commit that referenced this pull request Oct 3, 2025
… 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.
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