Warning: setState(...): Can only update a mounted or mounting component.
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
}
});
I'm pretty sure that's just a warning in React Native...but still worth investigating.
@awesomepankaj do you get the image back when this happens?
Any ideas how solved this problem?
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.
Someone as ideas how solved this problem?
Currently having this issue too.
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?
any update for this case?
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.
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.
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",
@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 yea, I'm pretty sure it was caused by the navigation in our case. Thanks for the clarification.
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.
Closing this issue after a prolonged period of inactivity. Fell free to reopen this issue, if this still affecting you.