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:
Darren Ethier 2019-11-22 15:18:45 -05:00 committed by GitHub
parent aa2e2d8f1e
commit 5c26249e26
1 changed files with 2 additions and 6 deletions

View File

@ -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;