gpt4 book ai didi

javascript - react native 的 setState 中的 key/label 是如何推断出来的?

转载 作者:行者123 更新时间:2023-11-30 21:17:21 26 4
gpt4 key购买 nike

我正在浏览 React Native Autocomplete Input example当我遇到一个经典的 this.setState 调用时,它没有合并到带有标签的对象中。

  constructor(props) {
super(props);
this.state = {
films: [],
query: ''
};
}

componentDidMount() {
fetch(`${API}/films/`).then(res => res.json()).then((json) => {
const { results: films } = json;
this.setState({ films });
});
}

我期待 this.setState 调用看起来像

this.setState({ films: films });

说明允许使用这种语法糖的代码/文档在哪里?这是纯 JavaScript 功能还是 React 功能?

相关研究

这感觉类似于object decomposition但“逆转”了。

我还仔细检查了 API 调用确实返回了一个类似这样的数组

"results": [ ... ] 

所以它不像this.setState({ films });实际上有 this.setState({ films: actual_object }); 的形式.

最佳答案

它来自Object literals shorthand .

this.setState({ films });
// is the same as:
this.setState({ films: films });

另见 http://es6-features.org/#PropertyShorthand用于语法糖。

关于javascript - react native 的 setState 中的 key/label 是如何推断出来的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45553771/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com