Skip to content

Turning an image into a button with ReactNative(TouchableOpacity)

   

This is a sample implementation of TouchableOpacity, which turns a view such as an image into a button in ReactNative.
Tap on an image of Kabigon to get an alert.

react-native: 0.56.0

alt

import React, {Component} from 'react';
import {AppRegistry} from 'react-native';
import {
StyleSheet,
Button,
TouchableOpacity,
View,
Alert,
Image
} from 'react-native';
import {name as appName} from './app.json';
export class App extends Component {
render() {
return (
<View style={styles.container}>
<TouchableOpacity
onPress={() => Alert.alert('Hello')}>
<Image
style={styles.image}
source={require('./image/kabigon.png')}
/>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
image: {
width: 250,
height: 250,
}
});
AppRegistry.registerComponent(appName, () => App);
view raw index.js hosted with ❤ by GitHub

  1. Display text in ReactNative
  2. A note on the commands that are only used occasionally in ReactNative
  3. Show the indicator in ReactNative
  4. Display an image with ReactNative
  5. Specifying the size of the View in FlexBox
  6. Displaying a FlatList in ReactNative
  7. Display the button with ReactNative