Warning: setState(...): Can only update a mounted or mounting component.
See original GitHub issueWhen 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
}
});
Issue Analytics
- State:
- Created 7 years ago
- Comments:15 (3 by maintainers)
Top Results From Across the Web
setState(...): Can only update a mounted or ... - Stack Overflow
Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component.
Read more >Prevent React setState on unmounted Component
Warning: Can only update a mounted or mounting component. This usually means you called setState, replaceState, or forceUpdate on an unmounted ...
Read more >Warning: setState(...): Can only update a mounted or ... - GitHub
"Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a...
Read more >React: Stop checking if your component is mounted - Medium
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your...
Read more >Prevent React setState on unmounted component - Martin Belev
Warning: Can only update a mounted or mounting component. This usually means you called setState, replaceState, or forceUpdate on an unmounted ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Someone as ideas how solved this problem?
Closing this issue after a prolonged period of inactivity. Fell free to reopen this issue, if this still affecting you.