1

I have an array of elements which was updated inside an action, now I want to update it in the store. Currently I have something like:

navigation
|_navigationItems:[{1:"foo"}, {2:"bar"}, {3:"foobar"}]

The thing is I was doing the following:

case types.UPDATE_NAVIGATION:
  return Object.assign({}, state, {
    navigationItems: action.payload.items,
  });

where items is: [{1:"zoo"}, {2:"foobar"}]

but store was not updated succesfully.

Did I miss something?

4
  • Is your action getting triggered. Can you console.log() within this case statement and see if you are getting the result Commented May 9, 2017 at 16:33
  • I put a console.log() just after the case is called and there I have the array updated. Commented May 9, 2017 at 16:39
  • 1
    Try the spread operator syntax: case types.UPDATE_NAVIGATION: return {...state, navigationItems: action.payload.items} Commented May 9, 2017 at 16:45
  • @ShubhamKhatri please, post it as an answer, it worked. But now: why did it work? Is still possible to do it with Object.assign? Commented May 9, 2017 at 16:48

1 Answer 1

1

React docs suggest on using the spread operator syntax over Object.assign

Use:

case types.UPDATE_NAVIGATION: 
     return {
         ...state, navigationItems: action.payload.items
     }
Sign up to request clarification or add additional context in comments.

2 Comments

Object.assign should have worked for you, you were doing it correctly as far as I can see.
You can refer this answer of mine as well stackoverflow.com/questions/43376849/… , provides more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.