I ran into an issue with react where pressing the browser "back" button on my inner page (thus going back to my outer page) doesn't fire the socket disconnect event. Refreshing, closing the browser, etc fire the disconnect event just fine.
I'm using react-router-dom like so:
const App = () => (
<Router>
<Route path="/" exact component={OuterComponent} />
<Route path="/innercomponent" component={InnerComponent} />
</Router>
);
I tried to fix this issue by firing socket disconnect on browser back in my InnerComponent:
useEffect(() => {window.onpopstate = e => {
socket.emit("disconnect");
socket.off();
console.log("socket disconnect should fire.");
};
}
But the disconnect event doesn't fire despite the console.log triggering when I click the back button. I'm assuming this issue is related to the react router.