Modules: util/assignIfChanged

Methods


assignIfChanged(target, newProps, compare)

Merges the properties of newProps into target, like Object.assign. However, each property is compared with the existing property on the target object. If the properties are equal, the property on the target object will not be overwritten.

_note: This logic is applied for each top-level property of newProps, but not for deeply nested properties. _

This util is useful when we want to cause as little mutations to the target object as possible. For example, in reducers mutations to objects will generally cause attached selectors and React components to update.

Parameters:
Name Type Description
target Object

The object to update properties on

newProps Object

An object containing new properties

compare function

The function that will be used to compare properties from newProps to existing properties on target. Defaults to the lodash isEqual util.

Source:
Returns:

A new object with the changed properties from newProps. If no properties have changed, will return a reference to the original object.

Type
Object