Before you read, I must say that I am aware that this is a common question here.
I have a modal that based on a prop that isOpen, will decide if the modal will be visible or not, but i need to keep track of the state of that modal so i can hide it after i am done using it, somehow i made it work in this form as it is now, but it shows me the warning 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 application. To fix, cancel all subscriptions and asynchronous tasks in %s.%s ...
I tried to search what is causing this problem and according to this it should be fixed, but I am still having that warning. I am new to react native and I don't fully understand what is happening. I would greatly appreciate if somebody can help me understand what the problem is and how to solve it.
export default class Update extends React.Component {
_isMounted = false;
constructor(props) {
super(props);
this.state = {
modalOpen: false,
name: this.props.name,
};
}
componentDidUpdate(prevProps) {
this._isMounted = true;
if (this._isMounted) {
if (this.props.isOpen !== prevProps.isOpen) {
this.setState({ modalOpen: true })
}
}
}
componentWillUnmount() {
this._isMounted = false;
}
update(updateObj, file) {
const { navigate } = this.props;
remove(file);
addFile(updateObj);
this.setState({ modalOpen: false });
navigate('Main');
}
render() {
const { name, modalOpen } = this.state;
const { closeModal, file } = this.props;
return (
<Modal
isOpen={modalOpen}
closeModal={closeModal}>
<TextInput
style={{ height: 60 }}
onChangeText={(name) => this.setState({ name })}
value={this.state.name}
/>
<Button
title="Update File"
onPress={
() => this.update({
name: this.state.name,
}, file)
}
/>
</Modal>
);
}
}