简体   繁体   English

在React / Redux中使用Object.assign或Spread Operator?哪个是更好的做法

[英]Use Object.assign or Spread Operator in React/Redux? Which is a better practise

I have some confusion about using Object.assign for React and Redux. 我对使用Object.assign for React和Redux感到困惑。

I read this article . 我读了这篇文章

It says ES6 Does not supported by all browsers but I have started using it. 它说ES6并不是所有浏览器都支持,但我已经开始使用它了。

I have two questions: 我有两个问题:

  1. Is it the right decision to continue with Object.assign ? 继续使用Object.assign是正确的决定吗?
  2. What is the alternative? 有什么选择?

My Code 我的守则

export const selectDiameter = (scaleData, size) => {
  return {
    type: SELECT_DIAMETER,
    payload: Object.assign({}, scaleData, {
         diameter: Object.assign({}, scaleData.diameter, {
             selected_tube_diameter: size,
             is_clicked: true,
             audio: Object.assign({}, scaleData.diameter.audio, {
               is_played: true
             })
         })
      })
   }
}

What is the alternative for the above code? 上述代码的替代方案是什么?

Redux docs recommends you to use the spread operator approach instead of the Object.assign Redux文档建议您使用扩展运算符方法而不是Object.assign

As per the docs: 根据文档:

An alternative approach is to use the object spread syntax proposed for the next versions of JavaScript which lets you use the spread (...) operator to copy enumerable properties from one object to another in a more succinct way. 另一种方法是使用为下一版JavaScript提出的对象扩展语法,它允许您使用spread(...)运算符以更简洁的方式将可枚举属性从一个对象复制到另一个对象。 The object spread operator is conceptually similar to the ES6 array spread operator 对象扩展运算符在概念上类似于ES6阵列扩展运算符

The advantage of using the object spread syntax becomes more apparent when you're composing complex objects 当您编写复杂对象时,使用对象扩展语法的优势变得更加明显

Using the Spread operator syntax 使用Spread运算符语法

export const selectDiameter = (scaleData,size) => {
  return {
    type: SELECT_DIAMETER,
    payload: {...scaleData, 
                 diameter: {
                          ...scaleData.diameter,
                           selectedTube: size,
                           isClicked: true,
                           audio: {
                                ...scaleData.diameter.audio,
                                isPlayed: true
                           }
                 }

             }
   }
}

It still uses ES6. 它仍然使用ES6。 See the Redux docs for more info on configuring your code for the same 有关为此配置代码的详细信息,请参阅Redux文档

However when you are dealing with the nested data. 但是,当您处理嵌套数据时。 I prefer to use immutability-helpers 我更喜欢使用immutability-helpers

For your code it will be like 对于你的代码,它就像

import update from 'immutability-helpers';

export const selectDiameter = (scaleData,size) => {
  return {
    type: SELECT_DIAMETER,
    payload: update(scaleData, {
         diameter: {
            selectedTube: { $set: size},
            isClicked: { $set: true},
            audio: {
                isPlayed: {$set: true}
            }
         }
    })
   }
}

The alternative to the code you posted is using object spreading: 您发布的代码的替代方法是使用对象传播:

export const selectDiameter = (scaleData, size) => ({
    type: SELECT_DIAMETER,
    payload: {
        ...scaleData,
        diameter: {
            ...scaleData.diameter,
            selected_tube_diameter: size,
            is_clicked: true,
            audio: {
                ...scaleData.diameter.audio,
                is_played:true
            }
        }
    }
});   

I have also shortened the body of the arrow function in your code to simply returning the object. 我还缩短了代码中箭头函数的主体,只需返回对象。

You can use the object rest spread syntax by using the Babel plugin transform-object-rest-spread . 您可以使用Babel插件transform-object-rest-spread来使用对象rest spread语法。

Install it like this: 像这样安装:

npm install --save-dev babel-plugin-transform-object-rest-spread

Configure its use in your .babelrc like this: .babelrc中配置它的用法,如下所示:

{
    "plugins": ["transform-object-rest-spread"]
}

Ok may be I was not really right but this is from Dan Abramov (redux creator) 好吧可能是我不是很对,但这是来自Dan Abramov(redux创作者)

Finally, you need to remember that Object.assign is a new method in ES6, so it is not natively available in all the browsers. 最后,您需要记住,Object.assign是ES6中的一种新方法,因此它并非在所有浏览器中都可用。 You should use a polyfill, either the one that ships with Babel or a standalone Object.assign polyfill, to use it without risking crashing your website. 您应该使用填充物(可以是Babel附带的填充物或独立的Object.assign polyfill)来使用它,而不会有使您的网站崩溃的风险。

Another option that doesn't require a polyfill is to use the new object spread operator, which is not part of ES6. 另一个不需要填充的选项是使用新的对象扩展运算符,它不是ES6的一部分。 However, it is proposed for ES7. 但是,它被提议用于ES7。 It is fairly popular, and it is enabled in Babel if you use the stage two preset. 它非常受欢迎,如果您使用第二阶段预设,它将在Ba​​bel中启用。

Object spread is ES7 syntax, render supported by Babel, Object.assign() is ES6 syntax, it not support in some browser. 对象传播是ES7语法,由Babel支持渲染,Object.assign()是ES6语法,在某些浏览器中不支持。 That's why you should use Object spread instead of Object.assign() 这就是为什么你应该使用Object spread而不是Object.assign()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM