javauser35
javauser35

Reputation: 1315

Update state object's property with Redux

I have init state like this:

  let initState = {
    checkBoxValues:{
        react: false,
        angular: false,
        vue: true,
        python: false,
        java: true
    }
  };

And my reducer :

export default (state = initState, action) => {
    switch(action.type){

    case "SET_CHECKBOX":
        let {name,checked}= action;

        return  {...state, checkBoxValues: ?????? }

    default:
        return state

    }
}

how can I update my state? For example name is "react" and checked is true.

  let {name,checked}= action;

Upvotes: 1

Views: 42

Answers (1)

Prakash Sharma
Prakash Sharma

Reputation: 16482

You can update it without mutation like this

return  {...state, checkBoxValues: {...state.checkBoxValues,[name]: checked } }

Upvotes: 1

Related Questions