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

Christopher Reyes picture Christopher Reyes · Nov 29, 2017 · Viewed 9k times · Source

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: 'Welcome',
  };
  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;

Answer

Brandon Mowat picture Brandon Mowat · Nov 29, 2017

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 component. If the props change, then the component will be re-rendered and variable will be re-created.