I am trying to use variables defined inside my React function in the window.addEventListner('beforeunload') function however it seems that the variables are already gone at that point. I need to do this to logout the user by calling a function inside my middleware called leaveRoom and pass in the variables "code" and "name". This is what I have tried:
...
149 window.addEventListener("beforeunload", e =>
150 {
151 if(!joined) return;
152 e.preventDefault();
153 leaveRoom({ code: code, username: name });
154 });
...
It is not the best way but you can define globals.
window.code = "code"
window.username = "Cem"
after that, you can access these variables from anywhere.