简体   繁体   English

在ES6中销毁对象作为参数

[英]Destructuring object as parameter in ES6

I am following a tutorial on react, and I am sorry for this newbie question but I am wondering why do we need to deconstruct parameters in this line, when we fetch data from an api: 我正在关注有关react的教程,对于这个新手问题,我感到很抱歉,但是我想知道为什么当我们从api获取数据时,为什么需要在此行中解构参数:

.then( ({results: items}) => this.setState({items}))

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

What I am wondering is, why can't we just pass the result of response.json() as a normal parameter like this: 我想知道的是,为什么我们不能只将response.json()的结果作为普通参数传递,如下所示:

.then( (items) => this.setState({items}))

I don't know the data structure of the reponse of that API, but assuming that that code is working, you "need" to destructure because the data object you're receving it's something like { results: items, .... }, but you don't want to do anything with the rest of the object. 我不知道该API响应的数据结构,但是假设该代码正在运行,则您“需要”进行解构,因为要接收的数据对象类似于{result:items,....} ,但您不想对对象的其余部分做任何事情。

Obviously, you don't need to do it like that in a strict way. 显然,您不需要严格地这样做。 You can do something like this result => this.setState({ items: result.items }) , but I think that you can agree with me that is a bit less compact. 您可以执行类似以下result => this.setState({ items: result.items }) ,但是我认为您可以同意我的观点,但该方法不太紧凑。

In the end is a question of preferences, but I personally prefer how the code looks with the destructuring. 最后是一个首选项的问题,但是我个人更喜欢代码在解构后的外观。

You dont need to destruct: 您不需要破坏:

.then( res => this.setState({items: res.result }))

or: 要么:

.then( response => response.json().result )
.then( items => this.setState({items}));

It's simple if you take a look of the response you will se that has an structure like the following. 如果您看一下具有以下结构的响应,这很简单。

{
 count: 0,
 results: ...
 ...
}

As you can see it's an object that one of its properties is called results . 如您所见,这是一个对象,其属性之一称为results In this particular case it seems you don't care about the other information you only want to grab the results which is an array. 在这种特殊情况下,您似乎并不关心仅想获取结果的其他信息(数组)。 If you were to just pass the return value from the callback without Destructuring you will set your state to a whole new object. 如果仅传递回调的返回值而不进行解构 ,则将状态设置为一个新对象。

Let's go step by step 让我们一步一步走

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

fetch returns a Promise that resolves into a Response object. fetch返回一个Promise,该Promise解析为Response对象。 response.json() returns a Promise that resolves into a JSON of the response body. response.json()返回一个Promise,该Promise解析为响应主体的JSON。 Every time that you return a Promise on a then() it's passed down the chain, so you get it as an input on the next then . 每次您在then()上返回Promise时,它都会沿链向下传递,因此您可以在下一个then上将其作为输入。 Finally, the JSON object that the response body contains it's probably of the form { results, ...} . 最后,响应主体包含的JSON对象的格式可能为{ results, ...} on the last fetch, the destructuring takes that JSON, extracts whatever the results key has inside and renames it to items , so you can reference it inside the callback as a variable called items . 在最后一次获取时,解构采用该JSON,提取results键内部包含的任何内容,并将其重命名为items ,因此您可以在回调内部将其引用为名为items的变量。

Without destructuring, that last then would need to have this shape: 如果没有解构,去年then需要有这样的形状:

.then(responseBodyAsJSON => this.setState({items: responseBodyAsJSON.results }))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM