Build: Counter Screen
A simple screen with a counter display and a button. Pressing the button increases the counter using a callback prop passed to the button component.
Target UI
Counter Screen Count: 0 [ Increase ]
Counter Screen Count: 0 [ Increase ]
import React, { useState } from 'react'; import { View, Text, TouchableOpacity, StyleSheet } from 'react-native'; function CounterButton(props) { // TODO: Use props.onPress as the button press handler return ( <TouchableOpacity style={styles.button} onPress={() => { /* TODO */ }}> <Text style={styles.buttonText}>Increase</Text> </TouchableOpacity> ); } export default function CounterScreen() { const [count, setCount] = useState(0); // TODO: Define a function to handle button press and update count return ( <View style={styles.container}> <Text style={styles.countText}>Count: {count}</Text> {/* TODO: Render CounterButton and pass the callback prop */} </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center' }, countText: { fontSize: 32, marginBottom: 20 }, button: { backgroundColor: '#007AFF', paddingVertical: 12, paddingHorizontal: 24, borderRadius: 8 }, buttonText: { color: 'white', fontSize: 18 } });
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.
Counter Screen Count: 1 [ Increase ]
callback props in React Native components?onPress from a parent to a child component in React Native?onPress={handlePress}, which is valid JSX syntax.function Parent() {
const handleClick = () => console.log('Clicked!');
return <Child onPress={handleClick} />;
}
function Child({ onPress }) {
return <Button title="Press me" onPress={onPress} />;
}handleClick as onPress prop to Child.onPress prop as the button's onPress handler, so pressing calls handleClick.function Parent() {
const handlePress = () => alert('Pressed');
return <Child onPress={handlePress()} />;
}
function Child({ onPress }) {
return <Button title="Click" onPress={onPress} />;
}onPress={handlePress()}, the function is called immediately, passing its result (undefined) instead of the function.onPress={handlePress}.Pass the test and this pattern moves unknown