@@ -55,14 +55,10 @@ export const useMutationWithMutationMode = <
5555 mode . current = mutationMode ;
5656 } , [ mutationMode ] ) ;
5757
58- const paramsRef = useRef < Partial < TVariables > > ( params ) ;
59- useEffect ( ( ) => {
60- paramsRef . current = params ;
61- } , [ params ] ) ;
6258 // This ref won't be updated when params change in an effect, only when the mutate callback is called (See L247)
6359 // This ensures that for undoable and optimistic mutations, the params are not changed by side effects (unselectAll for instance)
6460 // _after_ the mutate function has been called, while keeping the ability to change declaration time params _until_ the mutation is called.
65- const paramsAtExecutionTimeRef = useRef < Partial < TVariables > > ( params ) ;
61+ const paramsRef = useRef < Partial < TVariables > > ( params ) ;
6662
6763 // Ref that stores the snapshot of the state before the mutation to allow reverting it
6864 const snapshot = useRef < Snapshot > ( [ ] ) ;
@@ -213,16 +209,16 @@ export const useMutationWithMutationMode = <
213209 ...otherCallTimeOptions
214210 } = callTimeOptions ;
215211
212+ // store the hook time params *at the moment of the call*
213+ // because they may change afterwards, which would break the undoable mode
214+ // as the previousData would be overwritten by the optimistic update
215+ paramsRef . current = params ;
216+
216217 // Store the mutation with middlewares to avoid losing them if the calling component is unmounted
217218 if ( getMutateWithMiddlewares ) {
218219 mutateWithMiddlewares . current = getMutateWithMiddlewaresEvent (
219220 ( params : TVariables ) => {
220- // Store the final parameters which might have been changed by middlewares
221- paramsRef . current = params ;
222- return mutationFnEvent ( {
223- ...params ,
224- ...callTimeParams ,
225- } ) ;
221+ return mutationFnEvent ( params ) ;
226222 }
227223 ) ;
228224 } else {
@@ -236,14 +232,6 @@ export const useMutationWithMutationMode = <
236232 callTimeOnError . current = onError ;
237233 callTimeOnSettled . current = onSettled ;
238234
239- // store the hook time params *at the moment of the call*
240- // because they may change afterwards, which would break the undoable mode
241- // as the previousData would be overwritten by the optimistic update
242- paramsRef . current = params ;
243- // Also store them in a ref that will always be used for optimistic and undoable updates
244- // as their actual mutation happens after their side effects which may change the params (unselectAll for instance)
245- paramsAtExecutionTimeRef . current = params ;
246-
247235 if ( mutationMode ) {
248236 mode . current = mutationMode ;
249237 }
@@ -255,7 +243,7 @@ export const useMutationWithMutationMode = <
255243 }
256244
257245 snapshot . current = getSnapshotEvent (
258- { ...paramsAtExecutionTimeRef . current , ...callTimeParams } ,
246+ { ...paramsRef . current , ...callTimeParams } ,
259247 {
260248 mutationMode : mode . current ,
261249 }
@@ -264,13 +252,13 @@ export const useMutationWithMutationMode = <
264252 if ( mode . current === 'pessimistic' ) {
265253 if ( returnPromise ) {
266254 return mutation . mutateAsync (
267- { ...paramsAtExecutionTimeRef . current , ...callTimeParams } ,
255+ { ...paramsRef . current , ...callTimeParams } ,
268256 // We don't pass onError and onSettled here as we will call them in the useMutation hook side effects
269257 { onSuccess, ...otherCallTimeOptions }
270258 ) ;
271259 }
272260 return mutation . mutate (
273- { ...paramsAtExecutionTimeRef . current , ...callTimeParams } ,
261+ { ...paramsRef . current , ...callTimeParams } ,
274262 // We don't pass onError and onSettled here as we will call them in the useMutation hook side effects
275263 { onSuccess, ...otherCallTimeOptions }
276264 ) ;
@@ -285,7 +273,7 @@ export const useMutationWithMutationMode = <
285273
286274 // Optimistically update to the new value
287275 const optimisticResult = updateCacheEvent (
288- { ...paramsAtExecutionTimeRef . current , ...callTimeParams } ,
276+ { ...paramsRef . current , ...callTimeParams } ,
289277 {
290278 mutationMode : mode . current ,
291279 } ,
@@ -297,7 +285,7 @@ export const useMutationWithMutationMode = <
297285 if ( onSuccess ) {
298286 onSuccess (
299287 optimisticResult ,
300- { ...paramsAtExecutionTimeRef . current , ...callTimeParams } ,
288+ { ...paramsRef . current , ...callTimeParams } ,
301289 {
302290 snapshot : snapshot . current ,
303291 } ,
@@ -313,7 +301,7 @@ export const useMutationWithMutationMode = <
313301 ) {
314302 mutationOptions . onSuccess (
315303 optimisticResult ,
316- { ...paramsAtExecutionTimeRef . current , ...callTimeParams } ,
304+ { ...paramsRef . current , ...callTimeParams } ,
317305 {
318306 snapshot : snapshot . current ,
319307 } ,
@@ -329,7 +317,7 @@ export const useMutationWithMutationMode = <
329317 if ( mode . current === 'optimistic' ) {
330318 // call the mutate method without success side effects
331319 return mutation . mutate ( {
332- ...paramsAtExecutionTimeRef . current ,
320+ ...paramsRef . current ,
333321 ...callTimeParams ,
334322 } ) ;
335323 } else {
@@ -340,7 +328,7 @@ export const useMutationWithMutationMode = <
340328 if ( onUndo ) {
341329 onUndoEvent (
342330 {
343- ...paramsAtExecutionTimeRef . current ,
331+ ...paramsRef . current ,
344332 ...callTimeParams ,
345333 } ,
346334 {
@@ -355,7 +343,7 @@ export const useMutationWithMutationMode = <
355343 } else {
356344 // call the mutate method without success side effects
357345 mutation . mutate ( {
358- ...paramsAtExecutionTimeRef . current ,
346+ ...paramsRef . current ,
359347 ...callTimeParams ,
360348 } ) ;
361349 }
0 commit comments