import React, { useState } from 'react';
import { View, Text, Button, StyleSheet } from 'react-native';
const Greeting = ({ name = 'Friend' }) => {
return (
<Text style={styles.greetingText}>Hello, {name}!</Text>
);
};
const App = () => {
const [name, setName] = useState();
return (
<View style={styles.container}>
<Greeting name={name} />
<Button title="Change Name" onPress={() => setName('React Native Learner')} />
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
padding: 16
},
greetingText: {
fontSize: 24,
marginBottom: 20
}
});
export default App;We set a default value for the name prop directly in the function parameter using = 'Friend'. This means if no name is passed, it will show 'Friend'. When the button is pressed, the name changes to 'React Native Learner' and the greeting updates.
This is a simple way to provide default props in React Native functional components without extra code.