This is a really good question!
The short answer is: you cannot do a deep merge with the spread operator - it only does a shallow merge. But you can certainly write a function that will perform objects that intersect and implement deep merging.
This actually leaves you with 3 options:
1) Just do not make a deep merger. If you have a 2-level nested object, you can do such a simple thing:
const newNested = {...oldProps.nested, ...newProps.nested }; const finalProps = { ...oldProps, nested: newNested };
Incorrect merging is a force for you to say explicitly that you will have a new value for the nested property . This is good because it makes your code obvious. You can also try running the above examples here .
2) You can use the library for immutable structures. FE immutable.js . With it, your code will look pretty similar.
const oldProps = Immutable.fromJS({ nested: { prop1: "OldValue1", prop2: "OldValue2", } }); const newProps = Immutable.fromJS({ nested: { prop1: "NewValue1", } }); const finalProps = oldProps.updateIn(['nested'], (oldNested)=> oldNested.merge(newProps.get('nested')) )
3) You can use deep merging: find some implementation in npm or write it yourself and you will have such code (as an example, immutable.js example):
const finalProps = oldProps.mergeDeep(newProps);
In some cases, this may be required, but such code makes the update operation implicit , and it raises many problems, which are listed significantly here.