BlackH3art
BlackH3art

Reputation: 596

Why my reducer is adding new object to my redux store instead of changing current object values?

I'm having problem with React-Redux, I want to update specific values in my store, but instead I'm getting new object in my store.

My action type:

export const SENDMESSAGE = "SENDMESSAGE";

My action creator:

export const updateContactFormData = (payload) => ({
  type: SENDMESSAGE,
  payload: payload
})

My initial state:

export const formInitialData = {
  formShape: {...},
  formData: {...},
  formContactData: {
    name: '',
    email: '',
    text: ''
  }
}

And my reducer looking like this:

export default function formReducer(storeData, action) {

  switch(action.type) {

    case SENDMESSAGE:

      return {
        ...storeData,
        [storeData.formContactData]: action.payload,
      }

    default:
      return storeData || formInitialData
  }

}

And now im getting new object in my store, as the Redux tool show: enter image description here

Upvotes: 0

Views: 87

Answers (1)

Yury Tarabanko
Yury Tarabanko

Reputation: 45121

You want update property value, not to define a new property using previous value as a name.

return {
    ...storeData,
    formContactData: {...storeData.formContactData, ...action.payload},
  }

Upvotes: 1

Related Questions