Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

149
Vistas
What does Javascript Arrow function with object as parameter mean?

I was learning ReactJS here: https://egghead.io/lessons/react-dynamically-generated-components and came across this code snippet:

componentWillMount(){
    fetch( 'http://swapi.co/api/people/?format=json')
        .then( response => response.json() )
        .then( ({results: items}) => this.setState({items}) )
}

What does the ({results: items}) part of the arrow function mean?

I've seen arrow function

  • with no parameter ()=>console.log('hi')
  • without parenthesis word=>console.log(word)
  • and with multiple parameter seperated by comma (one, two)=>console.log(one)

But never an object literal in this way.

Also, why does this.setState({items}) need curly braces around items? What does it all mean?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

this.setState({items})

is ES6 shorthand for

this.setState({items: items})

and

({results: items}) 

basically accepts an object as a parameter with the property named results, and in the function body this is mapped to items

run trhough a transpiler, e.g. babel try it out repl page your code

fetch( 'http://swapi.co/api/people/?format=json')
    .then( response => response.json() )
    .then( ({results: items}) => this.setState({items}) )

becomes

var _this = this;

fetch('http://swapi.co/api/people/?format=json')
.then(function (response) {
    return response.json();
})
.then(function (_ref) {
    var items = _ref.results;
    return _this.setState({ items: items });
});

I would recommend keeping a link to that babel page - if any ES6 code confuses you, paste it there to see what the ES5 version is :p that's my cheat

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda