【问题标题】:Spread array destructuring in react-native erroredreact-native 中的扩展数组解构错误
【发布时间】:2017-01-15 00:47:03
【问题描述】:

以下代码:

const [ ...routes, currentRoute ] = props.routes

在该行上产生unexpected token 错误。

这是为什么?

【问题讨论】:

  • 语法错误不是“崩溃”。这只是一个语法错误。
  • 抱歉,同时编辑标题...

标签: javascript reactjs react-native ecmascript-6 babeljs


【解决方案1】:

您只能在解构表达式的末尾使用rest operator,例如:

const [ currentRoute, ...routes ] = props.routes.reverse()

它在ECMAScript 2015 Spec.#Destructuring Assignment 中声明,可能很难阅读,但请查看ArrayAssignmentPattern 结构。

【讨论】:

  • 那么,这只适用于解构?我已经看到并使用过其他扩展表达式,例如所示的here
  • @jsdario 它不是传播表达式。是rest赋值表达式,与rest参数非常相似。
猜你喜欢
  • 2020-10-13
  • 1970-01-01
  • 2021-03-09
  • 2020-11-01
  • 2016-10-07
  • 2018-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多