'Cannot read property 'launchImageLibrary' of undefined react-native-image-picker
My feature offers a small image icon as a touchable to upload or take a picture, using "react-native-image-picker": "^3.3.2".
I'm getting error: Cannot read property 'launchImageLibrary' of undefined, same as this GitHub issue but as you can see, my code already have the import as they told to.
Here's my full code:
import React from 'react';
import {
StyleSheet,
Image,
TouchableOpacity,
Alert
} from 'react-native';
import { launchImageLibrary } from 'react-native-image-picker';
const ImageUpload: React.FC<any> = ({}) => {
function showMessage() {
Alert.alert("Upload image", "Choose a option", [
{
text: 'Camera',
onPress: () => openCamera(),
},
{
text: 'Gallery',
onPress: () => openLibrary()
},
]);
}
const openLibrary = () => {
const options = {
storageOptions: {
skipBackup: true,
path: 'images',
},
};
launchImageLibrary(options, (response) => {
console.log(response);
});
}
const openCamera = () => {
//ongoing
}
return(
<>
<TouchableOpacity onPress={()=>showMessage()}>
<Image source={require('./picture.png')} style={{ width: 70, height: 70 }}/>
</TouchableOpacity>
</>
);
};
const style = StyleSheet.create({
ImageIcon: {
justifyContent: "center",
alignItems: "center",
}
});
export default ImageUpload;
Also, In VS Code, I have this error when calling launchImageLibrary:

I have already perfomed a npx pod-install.
Solution 1:[1]
It took me a very long time to get this to work but this worked for me.
< Button onPress = {
() =>
ImagePicker.launchImageLibrary({
mediaType: 'photo',
includeBase64: false,
maxHeight: 200,
maxWidth: 200,
},
(response) => {
console.log(response);
this.setState({
resourcePath: response
});
},
)
}
title = "Select Image" / >
References:
Solution 2:[2]
import Library as import * as ImagePicker from 'react-native-image-picker';
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tyler2P |
| Solution 2 | Volper |
