admin管理员组文章数量:1302230
I have the following autorun function in my ponentDidMount:
ponentDidMount() {
this.autoUpdate = autorun(() => {
this.setState({
rows: generateRows(this.props.data)
})
})
}
The problem is that another ponent changes this.props.data when the ponent is not mount - and therefor I get the .setState warning on an unmounted ponent.
So I would like to remove the autorun once the ponent unmounts.
I tried doing:
ponentWillUnmount() {
this.autoUpdate = null
}
But the autorun function still triggers. Is there a way to cancel the mobx autorun once the ponent is not mounted any more?
I have the following autorun function in my ponentDidMount:
ponentDidMount() {
this.autoUpdate = autorun(() => {
this.setState({
rows: generateRows(this.props.data)
})
})
}
The problem is that another ponent changes this.props.data when the ponent is not mount - and therefor I get the .setState warning on an unmounted ponent.
So I would like to remove the autorun once the ponent unmounts.
I tried doing:
ponentWillUnmount() {
this.autoUpdate = null
}
But the autorun function still triggers. Is there a way to cancel the mobx autorun once the ponent is not mounted any more?
Share Improve this question edited Apr 25, 2017 at 9:43 Tholle 113k22 gold badges208 silver badges197 bronze badges asked Apr 25, 2017 at 9:29 Miha ŠušteršičMiha Šušteršič 10.1k27 gold badges97 silver badges175 bronze badges1 Answer
Reset to default 11autorun
returns a disposer function that you need to call in order to cancel it.
class ExampleComponent extends Component {
ponentDidMount() {
this.autoUpdateDisposer = autorun(() => {
this.setState({
rows: generateRows(this.props.data)
});
});
}
ponentWillUnmount() {
this.autoUpdateDisposer();
}
render() {
// ...
}
}
本文标签: javascriptCanceling mobx autorun function on componentWillUnmountStack Overflow
版权声明:本文标题:javascript - Canceling mobx autorun function on componentWillUnmount - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741690123a2392672.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论