From 0536431308e8e1c3e9491ddd00af9ccb331c0e73 Mon Sep 17 00:00:00 2001 From: a01081440011 Date: Sun, 30 Oct 2022 13:57:24 +0900 Subject: [PATCH 1/2] fix: replace beforeunload to pagehide for safafi on ios --- .changeset/small-squids-wash.md | 5 +++++ contributors.yml | 1 + packages/react-router-dom/index.tsx | 10 +++++----- 3 files changed, 11 insertions(+), 5 deletions(-) create mode 100644 .changeset/small-squids-wash.md diff --git a/.changeset/small-squids-wash.md b/.changeset/small-squids-wash.md new file mode 100644 index 0000000000..b7570a0122 --- /dev/null +++ b/.changeset/small-squids-wash.md @@ -0,0 +1,5 @@ +--- +"react-router-dom": patch +--- + +Modified to use pagehide instead of beforeunload to work in the environment of safari on ios diff --git a/contributors.yml b/contributors.yml index c45e252b9d..2dd40ad218 100644 --- a/contributors.yml +++ b/contributors.yml @@ -128,3 +128,4 @@ - xavier-lc - xcsnowcity - yuleicul +- jakkku diff --git a/packages/react-router-dom/index.tsx b/packages/react-router-dom/index.tsx index 9c1e46fda3..537f2f3acc 100644 --- a/packages/react-router-dom/index.tsx +++ b/packages/react-router-dom/index.tsx @@ -1038,8 +1038,8 @@ function useScrollRestoration({ }; }, []); - // Save positions on unload - useBeforeUnload( + // Save positions on pagehide + usePagehide( React.useCallback(() => { if (navigation.state === "idle") { let key = (getKey ? getKey(location, matches) : null) || location.key; @@ -1109,11 +1109,11 @@ function useScrollRestoration({ }, [location, restoreScrollPosition, preventScrollReset]); } -function useBeforeUnload(callback: () => any): void { +function usePagehide(callback: () => any): void { React.useEffect(() => { - window.addEventListener("beforeunload", callback); + window.addEventListener("pagehide", callback); return () => { - window.removeEventListener("beforeunload", callback); + window.removeEventListener("pagehide", callback); }; }, [callback]); } From 29077f4e7c61b48991255cf02bc663089f3c661e Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Thu, 19 Jan 2023 13:50:35 -0500 Subject: [PATCH 2/2] Revert usage --- packages/react-router-dom/index.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/react-router-dom/index.tsx b/packages/react-router-dom/index.tsx index 955abfdb2c..ff43c18918 100644 --- a/packages/react-router-dom/index.tsx +++ b/packages/react-router-dom/index.tsx @@ -1143,8 +1143,8 @@ function useScrollRestoration({ }; }, []); - // Save positions on pagehide - usePagehide( + // Save positions on unload + useBeforeUnload( React.useCallback(() => { if (navigation.state === "idle") { let key = (getKey ? getKey(location, matches) : null) || location.key;