Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-native how to add image and onpress into touchable

I have touchable in react native, inside touchable I have image and on press like this

 <TouchableHighlight >
  <Image style={styles.imagestyle}
  source={require('./ic_action_name.png')} />
  onPress={() => this.moveToAddNewCustomer()}>
 </TouchableHighlight>

When I tried to run the app, I got this error

React.Childeren.only expected to receive a single React element child

How to fix this?

like image 907
Mr.moayed Avatar asked Nov 23 '16 09:11

Mr.moayed


People also ask

How do you add an image to touchable opacity in React Native?

If you want to add onPress prop to the image, then you have to use the TouchableOpacity component of React Native. Touchable Opacity has onPress prop and you can embed the Image component inside it. In the following example, we show an alert when the image is pressed.

How do you pass onPress in React Native?

To bind onPress with an argument in React Native, we can create a function that returns the onPress handler function. to add a Button with the onPress prop set to the function that we get after we call onPress with 'hello' .

How do I add an image to a view in React Native?

Adding Image Let us create a new folder img inside the src folder. We will add our image (myImage. png) inside this folder. We will show images on the home screen.

How do I add a background image in React Native to assets?

Approach One: Using React Native ImageBackground right from the react-native library and pass the desired styling and source image. Approach Two: Building a custom Background image component which will act flawlessly as a full width background image using a React Native Image and View.


4 Answers

You need to do it like this:

<TouchableHighlight onPress={() => this.moveToAddNewCustomer()}>
    <Image style={styles.imagestyle} source={require('./ic_action_name.png')} />
</TouchableHighlight>

or

<TouchableOpacity onPress={()=>this.moveToAddNewCustomer()}>
    <Image style={styles.imagestyle} source={require('./ic_action_name.png')} />
</TouchableOpacity>
like image 149
Venkatesh Somu Avatar answered Sep 25 '22 11:09

Venkatesh Somu


if

TouchableHighlight onPress={() => this.moveToAddNewCustomer()}>
    <Image style={styles.imagestyle} source={require('./ic_action_name.png')} />
</TouchableHighlight>

didn't work, you should put ur image in a <view>

like image 20
Mahgol Fa Avatar answered Sep 27 '22 11:09

Mahgol Fa


By default there is no click or events that attached with any of the Views in react native like react.js. There are mainly two Views specifically designed for handling touch events. They are TouchableOpacity and TouchableHighlight. You can resolve your issue by wrapping your Image component with TouchableOpacity or TouchableHighlight.

<TouchableHighlight onPress={() => this.moveToAddNewCustomer()}>
    <Image style={styles.imagestyle} source={require('./ic_action_name.png')} />
</TouchableHighlight>

https://reactnative.dev/docs/touchablehighlight

https://reactnative.dev/docs/touchableopacity

like image 24
Codemaker Avatar answered Sep 27 '22 11:09

Codemaker


I solved my issue by using "TouchableWithoutFeedback"

<TouchableWithoutFeedback
        onPress={() => {
           this.moveToAddNewCustomer()}
        }}
      >
        <Image
          style={styles.newImage}
          // resizeMode="contain"
          source={require("~/assets/images/test123.png")}
        />
      </TouchableWithoutFeedback>```
like image 38
Narayan Shrestha Avatar answered Sep 28 '22 11:09

Narayan Shrestha