1
我正在瀏覽React Native Autocomplete Input example,當時我遇到了一個經典的this.setState調用,它沒有在帶有標籤的對象中合併。推薦的鍵/標籤是如何響應本機的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 });
。
它是ES2015 [速記屬性名稱記法](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#New_notations_in_ECMAScript_2015) –