1

I used react-native-image-picker;

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

And when I top add img, but close window I had a error TypeError: undefined is not an object (evaluating 'response.assets[0]')

My code:

launchImageLibrary(options, (response) => {
  if (response) {            
    let base64 = response?.assets[0]?.uri;            
    dispatch({
      type: 'SET_PHOTO', 
      payload: {
        photo: base64, photoNumber, photoType,
      }
    })
  }
});

https://i.stack.imgur.com/wtOXg.png

What should I do? What kind of check to write? This only happens when you press button add photo from photo gallery, but changed my mind and closed the dialog window

Ahmet Firat Keler
  • 2,603
  • 2
  • 11
  • 22
Nik Silver
  • 11
  • 2

2 Answers2

0

You need to update the file path

const [filePath, setFilePath] = useState({
    assets: [
      {
        uri: 'You CAN place here any random image link',
      },
    ],
  });
0

I advice you a similar but more extensive library for this. It is react-native-image-crop-picker.

import ImagePicker from 'react-native-image-crop-picker';

After importing, you can use it like this (though you may need to tweak it, please console log returning image data)

ImagePicker.openPicker({
  width: 1536,
  height: 2048,
  includeBase64: true, // to get content as base64-encoded string 
  cropping: false,
}).then(image => {
    dispatch({
      type: 'SET_PHOTO', 
      payload: {
        photo: image, photoNumber, photoType,
      }
    });
})

Further reading

react-native-image-crop-picker

Ahmet Firat Keler
  • 2,603
  • 2
  • 11
  • 22