From 0b1bfffd36848c86ebf67fe14972708e6590c891 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Mon, 29 May 2023 10:42:41 +0100 Subject: [PATCH] Avoid mutations --- packages/core-data/src/reducer.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/packages/core-data/src/reducer.js b/packages/core-data/src/reducer.js index a12f0be244108..beac3205aaa15 100644 --- a/packages/core-data/src/reducer.js +++ b/packages/core-data/src/reducer.js @@ -507,7 +507,10 @@ export function undo( state = UNDO_INITIAL_STATE, action ) { const nextStack = [ ...stack ]; if ( existingEditIndex !== -1 ) { // If the edit is already in the stack leave the initial "from" value. - nextStack[ existingEditIndex ].to = to; + nextStack[ existingEditIndex ] = { + ...nextStack[ existingEditIndex ], + to, + }; } else { nextStack.push( { kind, @@ -555,14 +558,9 @@ export function undo( state = UNDO_INITIAL_STATE, action ) { } ); if ( isCachedChange ) { - let newCache = state.cache; - edits.forEach( - ( edit ) => - ( newCache = appendEditToStack( newCache, edit ) ) - ); return { ...state, - cache: newCache, + cache: edits.reduce( appendEditToStack, state.cache ), }; }