Objects

Learn how immutability principles are achieved with objects.

Object.assign() can be used to copy all the key/value pairs of one or more source objects into one target object. The method receives the following parameters:

  1. The target object to copy to.
  2. One or more source objects to copy from.

Since our reducers need to create a new object and make some changes to it, we will pass a new empty object as the first parameter to Object.assign(). The second parameter will be the original subtree to copy, and the third will contain any changes we want to make to the object. This will result in a new object with a new reference that contains the key/value pairs from the original state, and any overrides needed by the current action:

Get hands-on with 1400+ tech skills courses.