import React, { useState } from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';
function CounterButton(props) {
return (
<TouchableOpacity style={styles.button} onPress={props.onPress} accessibilityLabel="Increase counter button">
<Text style={styles.buttonText}>Increase</Text>
</TouchableOpacity>
);
}
export default function CounterScreen() {
const [count, setCount] = useState(0);
function handleIncrease() {
setCount(prevCount => prevCount + 1);
}
return (
<View style={styles.container}>
<Text style={styles.countText} accessibilityLiveRegion="polite">Count: {count}</Text>
<CounterButton onPress={handleIncrease} />
</View>
);
}
const styles = StyleSheet.create({
container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#fff' },
countText: { fontSize: 32, marginBottom: 20, color: '#000' },
button: { backgroundColor: '#007AFF', paddingVertical: 12, paddingHorizontal: 24, borderRadius: 8 },
buttonText: { color: 'white', fontSize: 18 }
});We created a CounterButton component that accepts an onPress callback prop. This prop is used as the handler for the button press event.
In the CounterScreen component, we keep track of the count state using useState. We define a function handleIncrease that increments the count.
This function is passed down to CounterButton as the onPress prop. When the button is pressed, it calls this function, updating the count in the parent.
We also added accessibility labels to improve usability for screen readers.