Scramblelock / starwars

A webpage of Star Wars characters created using React
1 stars 0 forks source link

Don't use array as a variable name #2

Open SoHotSoup opened 6 years ago

SoHotSoup commented 6 years ago

https://github.com/Scramblelock/starwars/blob/6658875849bb1fbde758f7d057616adf3da8ed97/src/containers/App.js#L21

In this case, I would rather use something like body or parsedResponse.

Also, because you expect the object with results at position 0, you can even write something like this:

Promise.all(urls.map(url => fetch(url).then(response => response.json())))
      .then([{ results }] => {
        this.setState({ people: results });
      })
      .catch(error => console.log("failed", error));

the idea behind this is to take advantage of Array and Object destructuring. First, you destruct and response to get the first object of the array, then you destruct object to get the results property.

SoHotSoup commented 6 years ago

Read this: https://dev.to/sarah_chima/destructuring-assignment---arrays-16f