最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - In React Native why is the const type used for in for example: const { navigate } = this.props.navigation;? - Stack

programmeradmin7浏览0评论

I am working on a project for school and using react native. Admittedly I am on the novice side with regards to JavaScript. I do not understand why in the React Navigation tutorial they use the const type.

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

And so my question is: Is there an particular or important reason why the const type is used for const { navigate } = this.props.navigation;

I am working on a project for school and using react native. Admittedly I am on the novice side with regards to JavaScript. I do not understand why in the React Navigation tutorial they use the const type.

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

And so my question is: Is there an particular or important reason why the const type is used for const { navigate } = this.props.navigation;

Share Improve this question asked Nov 29, 2017 at 21:15 Christopher ReyesChristopher Reyes 4811 gold badge5 silver badges11 bronze badges 1
  • Why not? The navigate function is not going to be altered in any way. It's simply a shorthand so you can write navigate('Chat') rather than the full identifier. – cmbuckley Commented Nov 29, 2017 at 21:23
Add a ment  | 

1 Answer 1

Reset to default 4

The const keyword is used when you want to create a Constant variable. Constants are like the variables you are probably used to, except they cannot be modified.

It's being used here because the variable is not being changed inside the render method of your ponent. If the props change, then the ponent will be re-rendered and variable will be re-created.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论