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.