R
R
ravshan selimov2020-07-21 20:56:00
React
ravshan selimov, 2020-07-21 20:56:00

How to partially change the state of redux?

Hi everyone, this is the current situation.

{
  ... ,
  user : { ... } ,
  ...
}


I send the name and new value of the property from the server,
and in the redux state, only one property of the user object needs to be changed,
how to do

function reducer(state, action){
  switch (action.type){
    ...

    case 'CHANGE_USER_DATA':{
      return {

        ...state,
        user : {
          ...state.user,
          action.prop : action.value
        }

      }
    }

    ...
  }
}

this is not how Unexpected token, expected "," works

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Shvedov, 2020-07-21
@ravshan01

return {
  ...state,
  user : {
    ...state.user,
    [action.prop] : action.value
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question