This might be very basic. On https://reactnative.dev/docs/intro-react they have shown examples with "export default" at the end, and definition of components before "export default" , e.g.:
import React from 'react';
import { Text, TextInput, View } from 'react-native';
const Cat = () => {
return (
<View>
<Text>I am also a cat!</Text>
</View>
);
}
const Cafe = () => {
return (
<View>
<Text>Welcome!</Text>
<Cat />
<Cat />
<Cat />
</View>
);
}
export default Cafe;
Later while reading https://reactnavigation.org/docs/params/, I noticed there seems to be a different format for the function component, with other components defined within curly brackets, like this:
function HomeScreen({ navigation }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<Button
title="Go to Details"
onPress={() => {
/* 1. Navigate to the Details route with params */
navigation.navigate('Details', {
itemId: 86,
otherParam: 'anything you want here',
});
}}
/>
</View>
);
}
I wonder whether the latter format is equivalent to that in the first example, and tried the following:
import React from 'react';
import { Text, TextInput, View } from 'react-native';
export default function App() {
const Cat = () => {
return (
<View>
<Text>I am also a cat!</Text>
</View>
);
}
const Cafe = () => {
return (
<View>
<Text>Welcome!</Text>
<Cat />
<Cat />
<Cat />
</View>
);
}
return(<Cafe/>);
}
Indeed the results are the same from test preview @ https://snack.expo.io/. Change the last return to return(<View><Cat/> <Cafe/> </View>);
, or change the name to export default function testApp() {
also works.
Although both work, but I don't know what are the differences/advantages for these two formats, and would like to know more.