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;
-
Why not? The
navigate
function is not going to be altered in any way. It's simply a shorthand so you can writenavigate('Chat')
rather than the full identifier. – cmbuckley Commented Nov 29, 2017 at 21:23
1 Answer
Reset to default 4The 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.