don’t mutate default state constant. (https://github.com/woocommerce/woocommerce-blocks/pull/1245)
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.
This commit is contained in:
parent
aa2e2d8f1e
commit
5c26249e26
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue