0

I'm new to react native and in my sample app I can't handle properly results returned from ImagePicker of this component https://github.com/react-native-image-picker/react-native-image-picker

I'm running react 0.65 and below is my code:

import * as ImagePicker from 'react-native-image-picker';

export class App extends Component {

constructor() {
    super();
    this.state = { imageSource: null };
}

selectImage = () => {
    const selectImageFromGallery = async () => {
      const response = await ImagePicker.launchImageLibrary('library', {
        selectionLimit: 1,
        mediaType: 'photo',
        includeBase64: true,
      }); 
      const {img64base} = response.assets[0];
      this.setState({img64base});
    };
     selectImageFromGallery(); 
    // console.log(resp); 
  }

render() {
    return (
      <SafeAreaView style={{justifyContent: 'center', alignItems: 'center'}}>
        <Button title='Select from gallery' onPress={() => this.selectImage()} />
        <Image source={this.state.imageSource} />
      </SafeAreaView>
    );
  };

}

Upon run of application I can press button and select image, but whenever I confirm my selection it is throwing error in console and on the screen of Android device:

Uncaught Error:
'Type Error: callback is not a function'
This call stack is not sybmolicated.

enter image description here

I do understand that I miss to handle correctly promise or callback but I cant figure out correct syntax. Could you please help? Tried zillion of times with 'await', without await, etc. The only thing I need to stay with component class and I won't change to function class - have single calls in componentDidMount functions to make sure some specific hardware is called only once. Pease help

1 Answers1

0
selectImage = async () {
      const response = await ImagePicker.launchImageLibrary('library', {
        selectionLimit: 1,
        mediaType: 'photo',
        includeBase64: true,
      }); 
      const {img64base} = response.assets[0];
      this.setState({img64base});
  }
The Scalion
  • 130
  • 6