Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to model a button with icons in react-native

I'm using react-native-icons package to include icons with buttons. They have a sample code listed in example folder. I'm trying to achieve onPress on View but turns out react-native doesn't have onPress function for <View> component.

I tried using <TouchableHighlight> but it can only have single child element in it not two like <Icon> and <Text> inside.

I also tried using <Text> with <Icon> and <Text> inside but <Text> can only have <Text> elements inside.

Has anyone have any luck achieving similar functionality ?

Sample Buttons with Icons

<View onPress={this.onBooking}    style={styles.button}>   <Icon     name='fontawesome|facebook-square'     size={25}     color='#3b5998'     style={{height:25,width:25}}/>   <Text style={styles.buttonText}>Sign In with Facebook</Text> </View> 
like image 776
Piyush Chauhan Avatar asked May 26 '15 02:05

Piyush Chauhan


People also ask

How do you add an icon to a button in react-native?

To add a button with icons in React Native, we can use the react-native-elements package to add the icon. To install it, we run npm i react-native-elements . to add a TouchableHighlight to provide the highlight effect for the button. Then we add the button content in the View .

How do you customize a button in react-native?

import { View, Button, StyleSheet, TouchableOpacity, Text } from "react-native"; To create custom buttons, you need to customize the <TouchableOpacity /> component and include the <Text /> component inside of it to display the button text.

Can we style button in react-native?

React Native's <Button /> component does not accept a style prop, and its color prop is limited and appears differently across Android, iOS, and the web. With the <Pressable /> component, we can create custom buttons that fit our app's design.


1 Answers

If you are using react-native-icons module, you can try wrap both your icon and text in a view, then use TouchableHighlight on top of it. Something like:

<TouchableHighlight onPress={()=>{}}>      <View>          <Icon ... />          <Text ... />      </View>  </TouchableHighlight> 

But it will be very easy if you use a relative new module react-native-vector-icons. You can do like:

<Icon name="facebook" style={styles.icon}>    <Text style={styles.text}>Login with Facebook</Text> </Icon> 
like image 72
Sean Zhao Avatar answered Sep 22 '22 11:09

Sean Zhao