This app shows a button to pick an image from the gallery. When the user picks one, it displays the image below the button.
import React, {useState} from 'react';
import {View, Button, Image, StyleSheet} from 'react-native';
import {launchImageLibrary} from 'react-native-image-picker';
export default function App() {
const [photoUri, setPhotoUri] = useState(null);
const pickImage = () => {
launchImageLibrary({mediaType: 'photo'}, (response) => {
if (!response.didCancel && !response.errorCode && response.assets && response.assets.length > 0) {
setPhotoUri(response.assets[0].uri);
}
});
};
return (
<View style={styles.container}>
<Button title="Pick an Image" onPress={pickImage} />
{photoUri && <Image source={{uri: photoUri}} style={styles.image} />}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
padding: 16
},
image: {
width: 200,
height: 200,
marginTop: 20,
borderRadius: 10
}
});