From 5c26249e263cd7bdb54ce8f33c0ba294ba14be6a Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Fri, 22 Nov 2019 15:18:45 -0500 Subject: [PATCH] =?UTF-8?q?don=E2=80=99t=20mutate=20default=20state=20cons?= =?UTF-8?q?tant.=20(https://github.com/woocommerce/woocommerce-blocks/pull?= =?UTF-8?q?/1245)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There was no need to have a const for the default state and also needed to make sure we’re not mutating it because it would result in the mutation of the default state. This in turn meant that on the initial reducing action, the default state in the store would have a ghost value. --- .../assets/js/data/query-state/reducers.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/plugins/woocommerce-blocks/assets/js/data/query-state/reducers.js b/plugins/woocommerce-blocks/assets/js/data/query-state/reducers.js index 999cbaf8d77..8301ec7fa75 100644 --- a/plugins/woocommerce-blocks/assets/js/data/query-state/reducers.js +++ b/plugins/woocommerce-blocks/assets/js/data/query-state/reducers.js @@ -4,24 +4,20 @@ import { ACTION_TYPES as types } from './action-types'; import { getStateForContext } from './utils'; -const DEFAULT_QUERY_STATE = {}; - /** * Reducer for processing actions related to the query state store. * * @param {Object} state Current state in store. * @param {Object} action Action being processed. */ -const queryStateReducer = ( state = DEFAULT_QUERY_STATE, action ) => { +const queryStateReducer = ( state = {}, action ) => { const { type, context, queryKey, value } = action; const prevState = getStateForContext( state, context ); let newState; switch ( type ) { case types.SET_QUERY_KEY_VALUE: const prevStateObject = - prevState !== null - ? JSON.parse( prevState ) - : DEFAULT_QUERY_STATE; + prevState !== null ? JSON.parse( prevState ) : {}; // mutate it and JSON.stringify to compare prevStateObject[ queryKey ] = value;