Introduction
Buttons let users tap to do something. Pressable is a flexible way to detect taps and other touch actions.
Buttons let users tap to do something. Pressable is a flexible way to detect taps and other touch actions.
import { Button, Pressable, Text, Alert } from 'react-native'; // Button usage <Button title="Click me" onPress={() => Alert.alert('Pressed!')} /> // Pressable usage <Pressable onPress={() => Alert.alert('Pressed!')}> <Text>Tap me</Text> </Pressable>
<Button title="Say Hello" onPress={() => Alert.alert('Hello!')} />
<Pressable onPress={() => Alert.alert('Pressed!')}>
<Text style={{color: 'blue'}}>Press me</Text>
</Pressable><Pressable
onPress={() => Alert.alert('Pressed!')}
style={({ pressed }) => [{
backgroundColor: pressed ? 'lightgray' : 'white'
}]}
>
<Text>Press me</Text>
</Pressable>This app shows a basic Button and a Pressable. Tapping either shows an alert. The Pressable changes color when pressed.
import React from 'react'; import { View, Button, Pressable, Text, Alert, StyleSheet } from 'react-native'; export default function App() { return ( <View style={styles.container}> <Button title="Click Button" onPress={() => Alert.alert('Button pressed!')} /> <Pressable onPress={() => Alert.alert('Pressable pressed!')} style={({ pressed }) => [ styles.pressable, { backgroundColor: pressed ? '#ddd' : '#eee' } ]} accessibilityLabel="Pressable button" > <Text style={styles.text}>Press Me</Text> </Pressable> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', padding: 20 }, pressable: { marginTop: 20, paddingVertical: 10, paddingHorizontal: 20, borderRadius: 5 }, text: { fontSize: 18, color: '#333' } });
Button is easy to use but less customizable.
Pressable lets you style and animate presses but needs more setup.
Always add accessibility labels for better screen reader support.
Button is for simple clickable buttons with default styles.
Pressable is for custom touchable areas with flexible styling.
Use Pressable when you want to change appearance on press or wrap complex content.
ButtonButton is designed for simple clickable buttons with default styles provided by React Native.Pressable is for custom touchable areas, View is a container, and TextInput is for text input, so they are not suitable for simple buttons.Button -> Option AButton [OK]Button, custom ones use Pressable [OK]Pressable in a React Native file?Pressable as named exports, so we use curly braces in import.react-native, not react-native-pressable.import React from 'react';
import { Pressable, Text, Alert } from 'react-native';
export default function App() {
return (
Alert.alert('Pressed!')}>
Click me
);
}Pressable wraps the Text and triggers onPress callback when pressed.onPress calls Alert.alert('Pressed!'), so an alert box will appear on press.Pressable:
import React from 'react';
import { Pressable, Text } from 'react-native';
export default function App() {
return (
console.log('Pressed')}>Click me
);
}PressableText component; raw strings are not allowed as direct children.onPress is correctly defined, imports are correct, and returning a single component is valid.Text component inside Pressable. -> Option BText wrapper [OK]Text inside Pressable [OK]Pressable to achieve this?Pressable accepts a style function with a pressed boolean to change styles when pressed.import { Pressable, Text } from 'react-native';
[{ backgroundColor: pressed ? 'gray' : 'white' }]}>
Tap me
uses the style function correctly to change background color on press. import { Button } from 'react-native';
({ backgroundColor: pressed ? 'gray' : 'white' })} /> incorrectly tries to style Button with a function. import { Pressable, Text } from 'react-native';
alert('Tapped')}>
Tap me
lacks style change. import { Pressable, Text } from 'react-native';
Tap me
sets static gray background without press effect.Pass the test and this pattern moves unknown