我正在浏览 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 });
关于javascript - react native 的 setState 中的 key/label 是如何推断出来的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45553771/