I want to send a cleanup request to my API sever when the user leaves the website (actually closes the tab/browser, not just changes page in the SPA). Is there an idiomatic way to do this in an app using React with React Router? Will componentWillUnmount fire on page close? I assume not.
Asked
Active
Viewed 4,858 times
1 Answers
3
React doesn't do any special thing here for you. Just do that you usually do for this. Also, see answers to a related question
The best place to put this subscription code is the componentDidMount
lifecycle method. And do not forget to unsubscribe it on componentWillUnmount
to prevent leaks.

Community
- 1
- 1

just-boris
- 9,468
- 5
- 48
- 84
-
Thank you very much, this solved my same issue by adding `window.onbeforeunload = this.function` in `componentDidMount` and `componentWillUnmount` – Huyen Sep 16 '19 at 22:02