jsdario jsdario - 1 year ago 95
React JSX Question

Spread array destructuring in react-native crashing

The following code:

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

Produces an
unexpected token
error o such line.

[Edit] Why is this?

Thank you.

Answer Source

You can use rest operator only at the end of the destructuring expression, e.g.:

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

It is stated in ECMAScript 2015 Spec.#Destructuring Assignment, it could be hard to read but look at ArrayAssignmentPattern structure.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download