Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Idiomatic way to handle website closed/leave page events with React/React Router

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.

like image 823
Migwell Avatar asked Oct 18 '22 19:10

Migwell


1 Answers

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.

like image 84
just-boris Avatar answered Oct 21 '22 17:10

just-boris