我从 react native 开始,当使用一个名为 react native paper 的库时,我遇到了一个语句,其中状态被分配给一个 const,如下所示。
import * as React from 'react';
import { Searchbar } from 'react-native-paper';
export default class MyComponent extends React.Component {
state = {
firstQuery: '',
};
render() {
const { firstQuery } = this.state;
return (
<Searchbar
placeholder="Search"
onChangeText={query => { this.setState({ firstQuery: query }); }}
value={firstQuery}
/>
);
}
}
'Render' 方法的开始,你可以看到 const { firstQuery } = this.state; 有人可以解释为什么将状态分配给名为“firstQuery”的常量,即使有原因,分配如何正确地将状态对象内的属性“firstQuery”映射到常量?
提前致谢。代码示例来自https://callstack.github.io/react-native-paper/searchbar.html#value