使用react-router-dom v6进行页面导航时处理浏览器的beforeunload事件。

huangapple go评论82阅读模式
英文:

Handle Browser beforeunload with react-router-dom v6 page navigation

问题

我有一个使用react-router v6构建的简单应用程序,我的用例是在页面卸载时使用最后更新的状态调用API。

  1. const handleBeforeUnload = () => {
  2. console.log("Running unload");
  3. };
  4. React.useEffect(() => {
  5. window.addEventListener("beforeunload", handleBeforeUnload);
  6. return () => window.removeEventListener("beforeunload", handleBeforeUnload);
  7. }, []);

在上述代码中,当我重新加载浏览器时,处理程序被调用。但是,当我尝试通过具有NavLink的侧边栏导航时,卸载不会被调用,因为react-router-dom执行客户端端路由,因此页面实际上从未被卸载。那么,如何在react-router-dom导航时在页面卸载时运行这段代码?

示例代码:https://codesandbox.io/s/lingering-pond-gt5u6d

英文:

I have a simple application with the navigation built with react-router v6. My use case is to call an API with the last updated state on page unload.

  1. const handleBeforeUnload = () => {
  2. console.log("Running unload");
  3. };
  4. React.useEffect(() => {
  5. window.addEventListener("beforeunload", handleBeforeUnload);
  6. return () => window.removeEventListener("beforeunload", handleBeforeUnload);
  7. }, []);

With the above code when I reload the browser the handler is getting called. But when I'm trying to navigate via sidebar having NavLink, the unload is not getting called because react-router-dom do client side routing so page is not actually never getting unload. So How can I run the piece of code on page unload with react-router-dom Navigation?

Sample Code:- https://codesandbox.io/s/lingering-pond-gt5u6d

答案1

得分: 0

以下是已翻译的内容:

You'll need to handle all the different ways a user leaves the component differently. Use beforeunload when the page is reloaded or closed, use an effect cleanup function to handle when the component is just unmounted.

Example:

  1. React.useEffect(() => {
  2. const handleBeforeUnload = () => {
  3. console.log("Running unload");
  4. };
  5. // handles when page is unloaded
  6. window.addEventListener("beforeunload", handleBeforeUnload);
  7. // cleanup function handles when component unmounts
  8. return () => {
  9. window.removeEventListener("beforeunload", handleBeforeUnload);
  10. handleBeforeUnload();
  11. };
  12. }, []);
英文:

You'll need to handle all the different ways a user leaves the component differently. Use beforeunload when the page is reloaded or closed, use an effect cleanup function to handle when the component is just unmounted.

Example:

  1. React.useEffect(() => {
  2. const handleBeforeUnload = () => {
  3. console.log("Running unload");
  4. };
  5. // handles when page is unloaded
  6. window.addEventListener("beforeunload", handleBeforeUnload);
  7. // cleanup function handles when component unmounts
  8. return () => {
  9. window.removeEventListener("beforeunload", handleBeforeUnload);
  10. handleBeforeUnload();
  11. };
  12. }, []);

huangapple
  • 本文由 发表于 2023年2月24日 05:18:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550412.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定