在 React Native 中为什么使用 const 类型,例如:const { navigate } = this.props.navigation;?

Chr*_*yes 5 javascript types react-native stack-navigator

我正在为学校做一个项目并使用 react native。诚然,我是 JavaScript 的新手。我不明白为什么在React Navigation 教程中他们使用 const 类型。

class HomeScreen extends React.Component {
  static navigationOptions = {
    title: 'Welcome',
  };
  render() {
    const { navigate } = this.props.navigation;
    return (
      <View>
        <Text>Hello, Chat App!</Text>
        <Button
          onPress={() => navigate('Chat')}
          title="Chat with Lucy"
        />
      </View>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是: const 类型用于 const { navigate } = this.props.navigation;

小智 4

const当您想要创建常量变量时​​,请使用该关键字。常量就像您可能习惯的变量一样,只是它们不能修改。

此处使用它是因为该变量未在render组件的方法内更改。如果发生props更改,则将重新渲染组件并重新创建变量。