Sai Krishnadas
Sai Krishnadas

Reputation: 3419

how to update a specific value in an object in redux

My Reducer :

   function updatePersonDetailsReducer (state = {
        updatePersonDetails: {
            name: "John",
            id: "3ery4",
            address: "somewhere on earth"
        }
    }, action) {
        switch (action.type) {
            case C.UPDATE_PERSON_DETAILS: {
                return {
                    ...state
                    ,updatePersonDetails :action.payload
                }
            }
            default : {}
        }
        return state
    }

I called this way in my component,

this.props.dispatch(updatePersonDetails({name:Variant.objValue.name,id:Variant.objValue.Id}))

But this seems to, create a new array than overwriting the values in the current array (updatePersonDetails) in reducer.

Upvotes: 1

Views: 764

Answers (1)

sakshya73
sakshya73

Reputation: 7212

You can spread your object and do it like this.

 case C.UPDATE_PERSON_DETAILS: {
                return {
                    ...state,
                    updatePersonDetails : 
                    {...state.updatePersonDetails,...action.payload}
                }
            }

This will keep your old keys there and will only update the newly added keys.

Upvotes: 5

Related Questions