In react, will componentWillUnmount trigger when the browser is closed?

Will

componentWillUnmount trigger when the browser is closed? Alert is not triggered when the browser is closed.
the current project requirement is to save user data to localStorage when the user logs out and logs in. In which life cycle should it be written?
ask for advice

Apr.09,2021

will not, please use the beforeunload event.


It doesn't matter whether

will or not.

the important thing is that you shouldn't write in componentWillUnmount.

the reason is that the behavior of browser shutdown may not be executed in componentWillUnmount at all, and the request may not be sent in time.

so it can be controlled in onbeforeunload.


No, browsers are closed (tabs are closed) to listen for events such as onbeforeunload. Your requirement should be that you save data when dealing with logout logic


those who answer may not have personally tested, refresh the page will still trigger the onbeforeunload event, have you tried, or tried to avoid this problem.?.

Menu