Typically functional components will take a props
object as an agrument. props
might have any number of key value pairs. For example, you might have props.navigation
. But writing function HomeScreen( navigation )
is not the same as writing function HomeScreen({ navigation })
. Writing { navigation }
instead of props
in the argument desctuctures the value from its parent object. If you wanted to avoid destructuring, you would write it like this:
function HomeScreen( props ) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
title="Go to Details"
onPress={() => props.navigation.navigate('Details')}
/>
</View>
);
}
Again, a react functional component expects to recieve the props object as the argument. We usually call it props
, but it could be called anything. This next block is identical, we're just naming props
something else:
function HomeScreen( swissArmyKnife ) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
title="Go to Details"
onPress={() => swissArmyKnife .navigation.navigate('Details')}
/>
</View>
);
}
For just one parameter, you're right, it's probably not necessary to destructure. Its just a shorthand. But when you have a props
object with many many parameters, and you want to pull only certain values from it, it makes more sense. like lets say your props
object had parameters navigation
, trains
, busses
, airplanes
, bikes
, skateboards
, and you just want to use a few in your component:
function HomeScreen( {busses, bikes} ) {
// do some stuff here with busses and bikes, don't worry about the whole props object
}