react-native-image-picker icon indicating copy to clipboard operation
react-native-image-picker copied to clipboard

Warning: setState(...): Can only update a mounted or mounting component.

Open mountainfirefly opened this issue 9 years ago • 15 comments

When I am clicking on the photo in the gallery then the app is getting relaunch and giving me the setState error.

react-native version: 0.41.2 react-native-image-picker: 0.25.5 testing on redmi android 5.1.1

code is this

import React from 'react';
import _ from 'lodash';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  PixelRatio,
  TouchableOpacity,
  Image
}
from 'react-native';

var ImagePicker = require('react-native-image-picker');

export default class App extends React.Component {

  constructor(props) {
    super(props);
    this.imageCheckInterval = null;
    this.state = {
      avatarSource: null,
      videoSource: null,
    };
  }

  selectPhotoTapped() {
    const options = {
      title: 'Select a Photo',
      cancelButtonTitle: 'Cancel',
      takePhotoButtonTitle: 'Take Photo…',
      chooseFromLibraryButtonTitle: 'Choose from Library…',
      quality: 1.0,
      allowsEditing: false
    }

    ImagePicker.showImagePicker(options, (response) => {
      console.log('Response = ', response, 'ww', this.state);

      if (response.didCancel) {
        console.log('User cancelled photo picker');
      }
      else if (response.error) {
        console.log('ImagePicker Error: ', response.error);
      }
      else if (response.customButton) {
        console.log('User tapped custom button: ', response.customButton);
      }
      else {
        let source = { uri: response.uri };

        // You can also display the image using data:
        // let source = { uri: 'data:image/jpeg;base64,' + response.data };


            this.setState({
              avatarSource: source
            });
          
      }
    });
  }

  selectVideoTapped() {
    const options = {
      title: 'Video Picker',
      takePhotoButtonTitle: 'Take Video...',
      mediaType: 'video',
      videoQuality: 'medium'
    };

    ImagePicker.showImagePicker(options, (response) => {
      console.log('Response = ', response);

      if (response.didCancel) {
        console.log('User cancelled video picker');
      }
      else if (response.error) {
        console.log('ImagePicker Error: ', response.error);
      }
      else if (response.customButton) {
        console.log('User tapped custom button: ', response.customButton);
      }
      else {
        this.setState({
          videoSource: response.uri
        });
      }
    });
  }

  render() {
    console.log(this.state.avatarSource)
    return (
      <View style={styles.container}>
        <TouchableOpacity onPress={this.selectPhotoTapped.bind(this)}>
          <View style={[styles.avatar, styles.avatarContainer, {marginBottom: 20}]}>
          { this.state.avatarSource === null ? <Text>Select a Photo</Text> :
            <Image style={styles.avatar} source={this.state.avatarSource} />
          }
          </View>
        </TouchableOpacity>

        <TouchableOpacity onPress={this.selectVideoTapped.bind(this)}>
          <View style={[styles.avatar, styles.avatarContainer]}>
            <Text>Select a Video</Text>
          </View>
        </TouchableOpacity>

        { this.state.videoSource &&
          <Text style={{margin: 8, textAlign: 'center'}}>{this.state.videoSource}</Text>
        }
      </View>
    );
  }

  whenClicked = (items) => {
    console.log(items)
  } 

}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF'
  },
  avatarContainer: {
    borderColor: '#9B9B9B',
    borderWidth: 1 / PixelRatio.get(),
    justifyContent: 'center',
    alignItems: 'center'
  },
  avatar: {
    borderRadius: 75,
    width: 150,
    height: 150
  }
});

mountainfirefly avatar Feb 23 '17 19:02 mountainfirefly

I'm pretty sure that's just a warning in React Native...but still worth investigating.

marcshilling avatar Feb 24 '17 05:02 marcshilling

@awesomepankaj do you get the image back when this happens?

haitaoli avatar Apr 20 '17 03:04 haitaoli

Any ideas how solved this problem?

BroshkovD avatar Jun 12 '17 14:06 BroshkovD

happening with me also when I run the app in Android 5.x, the moment image picker opens up, the component gets unmounted. Now when I select the image, it is passed to the callback function which tries to update the state using setState, but the app goes to its initial state with the warning that you tried to call setState on an unmounted component.

Strange thing is its not happening in Android 6+ devices but only with Android 5 alone.

sahil290791 avatar Dec 19 '17 12:12 sahil290791

Someone as ideas how solved this problem?

thadeu avatar Dec 20 '17 12:12 thadeu

Currently having this issue too.

bz123 avatar Dec 22 '17 09:12 bz123

We are also seeing this with newer versions:

react-native version: 0.44.0 react-native-image-picker: 0.26.7 testing on android 8.0.0

IOS works fine. Android, the component unmounts when we call launchImageLibrary. Have tried to code around it, launching in a modal or a scene, no luck so far. Anyone had any success using this on android?

leegould avatar Dec 22 '17 09:12 leegould

any update for this case?

riosimatupang avatar Jan 22 '18 16:01 riosimatupang

I think for us this problem seemed to be caused by trying to upgrade our navigation (react-native-router-flux) to v4 from v3. It caused this problem in many libraries on android. We rolled back the RNRF update, I guess it might be an issue in that, or the library it is based on, react-navigation. Both are in beta.

leegould avatar Jan 22 '18 16:01 leegould

I looked into this issue before but did not find a solution. On iOS, the controller stays in memory even if the view is temporarily removed. But on Android activities are serialized and unmounted if device is low on memory when camera app is launched. After the activity is re-mounted, it cannot restore the callbacks etc., then onActivityResult cannot deliver data to js code.

haitaoli avatar Jan 22 '18 22:01 haitaoli

I have same issue too I think maybe from react-native-router-flux. "react-native": "0.50.4", "react-native-router-flux": "^4.0.0-beta.28",

pilawalt avatar Jan 29 '18 07:01 pilawalt

@leegould I am using react native 0.44.3, image-picker: 0.26.3 and it works in all versions including Android 8.x except Android 5.x.

sahil290791 avatar Jan 31 '18 09:01 sahil290791

@sahil290791 yea, I'm pretty sure it was caused by the navigation in our case. Thanks for the clarification.

leegould avatar Jan 31 '18 09:01 leegould

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You may also mark this issue as a "discussion" and I will leave this open.

stale[bot] avatar Jun 11 '19 16:06 stale[bot]

Closing this issue after a prolonged period of inactivity. Fell free to reopen this issue, if this still affecting you.

stale[bot] avatar Jun 18 '19 17:06 stale[bot]