admin管理员组文章数量:1426029
I have a ponent that will call browserHistory.push(url) at some point, and I need to call a function after the ponent has been unmounted and the next ponent has mounted. At the moment, I'm using setTimeout because it seems that unmounting a ponent fires all the setTimeouts immediately. Is there a better way of doing this?
I have a ponent that will call browserHistory.push(url) at some point, and I need to call a function after the ponent has been unmounted and the next ponent has mounted. At the moment, I'm using setTimeout because it seems that unmounting a ponent fires all the setTimeouts immediately. Is there a better way of doing this?
Share Improve this question asked Oct 15, 2018 at 10:22 SJCSJC 1072 silver badges12 bronze badges 1- Please provide your code for better understanding – Jozef Cipa Commented Oct 15, 2018 at 10:30
3 Answers
Reset to default 3I'm not sure what that function does, but the best is to call it within the ponentDidMount
in your next ponent.
You should go through the lifecycle docs of react https://reactjs/docs/state-and-lifecycle.html
The main two methods are for your use is ponentDidMount. You can call your function inside this method of the next ponent that renders after pushing the URL. It is obvious that your old ponent was unmounted at that time.
I can only suggest this approach at this time. Maybe your function has different dependencies which require its call in the old ponent itself.
You can use your parent ponent for calling that function, if you do conditional rendering.
You can also use ponentDidMount
in next ponent as @Hasan said.
本文标签: javascriptHow to call a function after a component has been unmountedStack Overflow
版权声明:本文标题:javascript - How to call a function after a component has been unmounted? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745408355a2657352.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论