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;
const is just the best to use in this scenario because you want to declare a constant and not allow it to be overwritten.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With