最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to call a function after a component has been unmounted? - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 3

I'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.

发布评论

评论列表(0)

  1. 暂无评论