Cannot read property 'launchImageLibrary' of undefined react-native-image-picker

Viewed 2251

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: enter image description here

I have already perfomed a npx pod-install.

3 Answers

i use "react-native-image-picker": "^4.8.5",

i just restart metro "npx react-native start" or "npx react-native start --reset-cache"

and rerun metro bundler "npx react-native run-android"

my code is

import { launchImageLibrary } from "react-native-image-picker";

 handleChangePhoto = () => {
    launchImageLibrary({ noData: true }, (response) => {
      console.log(response);
      // if (response) {
      //   setPhoto(response);
      // }
    });
  };
import Library as import * as ImagePicker from 'react-native-image-picker';
Related