5 Different Ways to Create Buttons in React Native: A Complete Guide
In React Native, you can create buttons using various methods depending on your styling needs and functionality. Below are different approaches with examples:
1. Using Button Component (Built-in)
The simplest way is to use the Button component provided by React Native.
import React from 'react'; import { Button, View, Alert } from 'react-native'; const App = () => { return ( <View style={{ margin: 20 }}> <Button title="Press Me" onPress={() => Alert.alert('Button Pressed')} /> </View> ); }; export default App;
2. Using TouchableOpacity for Customizable Buttons
TouchableOpacity allows you to create more customizable buttons with different styles.
import React from 'react'; import { TouchableOpacity, Text, StyleSheet, Alert } from 'react-native'; const CustomButton = () => { return ( <TouchableOpacity style={styles.button} onPress={() => Alert.alert('Custom Button Pressed')}> <Text style={styles.buttonText}>Press Me</Text> </TouchableOpacity> ); }; const styles = StyleSheet.create({ button: { backgroundColor: '#007bff', padding: 10, borderRadius: 5, alignItems: 'center', }, buttonText: { color: '#fff', fontSize: 16, }, }); export default CustomButton;
3. Using TouchableHighlight for Highlight Effects
TouchableHighlight gives a visual feedback (highlight) when the button is pressed.
import React from 'react'; import { TouchableHighlight, Text, StyleSheet, Alert } from 'react-native'; const HighlightButton = () => { return ( <TouchableHighlight style={styles.button} underlayColor="#0056b3" onPress={() => Alert.alert('Highlight Button Pressed')} > <Text style={styles.buttonText}>Press Me</Text> </TouchableHighlight> ); }; const styles = StyleSheet.create({ button: { backgroundColor: '#007bff', padding: 10, borderRadius: 5, alignItems: 'center', }, buttonText: { color: '#fff', fontSize: 16, }, }); export default HighlightButton;
4. Using TouchableWithoutFeedback for Custom Gestures
TouchableWithoutFeedback provides even more flexibility, but you need to manage your own styles and feedback.
import React from 'react'; import { TouchableWithoutFeedback, Text, View, StyleSheet, Alert } from 'react-native'; const FeedbackButton = () => { return ( <TouchableWithoutFeedback onPress={() => Alert.alert('Button Pressed')}> <View style={styles.button}> <Text style={styles.buttonText}>Press Me</Text> </View> </TouchableWithoutFeedback> ); }; const styles = StyleSheet.create({ button: { backgroundColor: '#28a745', padding: 10, borderRadius: 5, alignItems: 'center', }, buttonText: { color: '#fff', fontSize: 16, }, }); export default FeedbackButton;
5. Using Pressable for More Advanced Touch Handling
Pressable gives you full control over touch events, allowing you to respond to different states like press, hover, etc.
import React from 'react'; import { Pressable, Text, StyleSheet, Alert } from 'react-native'; const PressableButton = () => { return ( <Pressable style={({ pressed }) => [ { backgroundColor: pressed ? '#0056b3' : '#007bff', }, styles.button, ]} onPress={() => Alert.alert('Pressable Button Pressed')} > <Text style={styles.buttonText}>Press Me</Text> </Pressable> ); }; const styles = StyleSheet.create({ button: { padding: 10, borderRadius: 5, alignItems: 'center', }, buttonText: { color: '#fff', fontSize: 16, }, }); export default PressableButton;
Each method offers different flexibility, with the built-in Button being the easiest but least customizable, while TouchableOpacity, TouchableHighlight, and Pressable allow for deeper control over the button’s appearance and behavior.