jsdario jsdario - 2 months ago 25
React JSX Question

Spread array destructuring in react-native errored

The following code:

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


Produces an
unexpected token
error on such line.

Why is this?

Answer

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.