EEE
EEE

Reputation: 149

Accessing a nested key inside reducer

hard for me to word this question. but how do I access a nested key inside redux? I'd like to apply the spread operator to the key "appointment" in 'FETCH_APPOINTMENT, but i don't know how to access it. Thanks

what i tried

case 'FETCH_APPOINTMENT':
  return { ...state, appointment: action.payload.data };

case 'SOME_CASE':
  return {
    [state && state.appointment]: state && state.appointment,
    ..._.mapKeys(action.payload.data, 'id'),
  };
  result
  {undefined: undefined}

the results are them not being able to define state.appointment

Upvotes: 0

Views: 44

Answers (1)

Chase DeAnda
Chase DeAnda

Reputation: 16441

This should work. You can remove the state && state.appointment truthy check by adding a default value for appointment in your reducers initialState:

const initialState = {
  appointment: 'A'
};

const reducers = (state = initialState, action) => {
  switch(action) {
    case "SOME_CASE": {
      return {
        [state.appointment]: {
          ...[state.appointment],
          _.mapKeys(action.payload.data, 'id')
        }
      }
    }
  }
}

Upvotes: 1

Related Questions