Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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;

like image 967
Christopher Reyes Avatar asked Nov 29 '17 21:11

Christopher Reyes


People also ask

Why do we use const in react native?

const is just the best to use in this scenario because you want to declare a constant and not allow it to be overwritten.

Why is const necessary?

The const keyword allows you to specify whether or not a variable is modifiable. You can use const to prevent modifications to variables and const pointers and const references prevent changing the data pointed to (or referenced).

What does const stand for in react?

const is short for contant, which indicates the variable's value won't change. let is the opposite, meaning that the variable's value will change. and var is just neutral between the two.


1 Answers

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.

like image 190
Brandon Mowat Avatar answered Sep 22 '22 13:09

Brandon Mowat