From 4430ae546aced1d478311cdafd01365b948b1d81 Mon Sep 17 00:00:00 2001 From: Peter Almon Date: Fri, 2 Aug 2019 09:44:43 -0400 Subject: [PATCH] Update UsingObjectSpreadOperator.md (#3367) I think this warning on using the Object spread operator to clone objects is important. While, good style would encourage the use of flat objects in the reducer, not everyone is going to do that. Using the spread syntax in a reducer on a multidimensional data structure is opening up the door to state mutation. --- docs/recipes/UsingObjectSpreadOperator.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/recipes/UsingObjectSpreadOperator.md b/docs/recipes/UsingObjectSpreadOperator.md index a13c90e29c1..cb129f33158 100644 --- a/docs/recipes/UsingObjectSpreadOperator.md +++ b/docs/recipes/UsingObjectSpreadOperator.md @@ -64,3 +64,6 @@ While the object spread syntax is a [Stage 4](https://github.com/tc39/proposal-o "plugins": ["@babel/plugin-proposal-object-rest-spread"] } ``` +> ##### Note on Object Spread Operator + +> Like the Array Spread Operator, the Object Spread Operator creates a [shallow clone](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#Spread_in_object_literals) of the original object. In other words, for multidimensional source objects, elements in the copied object at a depth greater than one are mere references to the source object (with the exception of [primitives](https://developer.mozilla.org/en-US/docs/Glossary/Primitive), which are copied). Thus, you cannot reliably use the Object Spread Operator (`...`) for deep cloning objects.