I got an error when open dialog from another class component: "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 the componentWillUnmount method"
index.js
import ...
class AdMenu extends Component {
componentWillMount = () => {
this.onSearch();
};
onOpenInsert = () => {
showDetailDialog();
};
onSearch = () => {
fetch(_url, ...)
.then(response => {
if (response.ok) {
return response.json();
} else {
throw response;
}
})
.then(responseJson => {
this.setState({...});
})
.catch(response => {...});
};
render() {
return (
<div>
<DetailDialog />
<Button color="primary" onClick={this.onOpenInsert}>Add New</Button>
<BootstrapTable .... />
</div>
);
}
}
export default withTranslation()(AdMenu);
DetailDialog.js
export var showDetailDialog = function () {
this.setState({open: true});
console.log('Mounted: ' + this.mounted);
};
class DetailDialog extends React.Component {
mounted = false;
controller = new AbortController();
constructor(props) {
super(props);
this.state = {open: false};
showDetailDialog = showDetailDialog.bind(this);
}
componentDidMount() {
console.log('componentDidMount');
this.mounted = true;
}
componentWillUnmount(){
console.log('componentWillUnmount');
this.mounted = false;
}
onClose = () => {
this.setState({open: false});
};
render() {
return (
<Modal isOpen={this.state.open} toggle={this.onClose} className={"modal-primary"} >
<ModalHeader toggle={this.onClose}>Detail</ModalHeader>
<ModalBody>
...
</ModalBody>
</Modal>
);
}
}
export default withTranslation()(DetailDialog);
I have a DetailDialog exported class component and function showDetailDialog. It imported to index.js page.
When I open page in the first time and click open dialog then work fine. But when I switch to another page by Router in menu then open again page in the second time, I got an error in console log.
I tried use this.mounted var to check unmounted component, but I don't know How to set state to open detail dialog when component had unmount in the second time and next.
I tried use controller = new AbortController(); and controller.abort() in componentWillUnmount() but not working.
Or any solution for this problem?
Thanks!
Image: https://prnt.sc/nsp251
Source on CodeSandbox: https://codesandbox.io/s/coreuicoreuifreereactadmintemplate-5unwj
Step test:
Click Ad Menu (1 st)
Click Ad Group
Click Ad Menu (2 nd)
Click Open Dialog in Ad Menu
View Console log browser
File: src/views/category
Node v11.12.0
Npm 6.7.0
Window 10