Spread Operator not working for Redux/ES6 based sample

只谈情不闲聊 提交于 2019-12-05 08:18:59

The object spread syntax is not supported in most browsers at the minute. It's proposed for addition in ES7 (aka ES2016). As far as I know there's no way to polyfill it, as it uses a new syntax rather than just being a function call.

You have two options in the meantime.

1) Use Object.assign to create an updated version of the object, like so:

Object.assign({}, state, {
  completed: !state.completed
});

Although this will also need to be polyfilled in most browsers - a good example one is available on MDN, or you can use a third party library's version, like the one in lodash.

2) Use transpiling tools like Babel, which allow you to write your code with newer syntax and then convert it to a version that works in all browsers.

If anyone using Babel is still having trouble, this feature may not be available with Babel out of the box, you may need to add a plugin: http://babeljs.io/docs/plugins/transform-object-rest-spread/

then update .babelrc with

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

You can not polyfill syntax. You need to use something like babel to compile to an older version of JavaScript if you wish to execute in current browsers.

https://babeljs.io/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!