import React, { useState } from 'react';
import { View, Text, Button, StyleSheet } from 'react-native';
export default function GreetingScreen() {
const [greeting, setGreeting] = useState('Hello, User!');
const changeGreeting = () => {
setGreeting('Welcome to React Native!');
};
return (
<View style={styles.container}>
<Text testID="greetingText" style={styles.greetingText}>{greeting}</Text>
<Button title="Change Greeting" onPress={changeGreeting} />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
greetingText: {
fontSize: 24,
marginBottom: 20,
},
});This component uses React Native's useState hook to keep track of the greeting message. Initially, it shows "Hello, User!". When the user taps the "Change Greeting" button, the changeGreeting function updates the state to "Welcome to React Native!", which updates the displayed text.
We added a testID to the Text component to make it easy to find in tests.
Component tests should check that the initial text is correct and that pressing the button changes the text as expected.