Просмотр исходного кода

add fn to replace url in address bar with new path

yohei0125 3 лет назад
Родитель
Сommit
4632384425
1 измененных файлов с 14 добавлено и 4 удалено
  1. 14 4
      packages/app/src/components/NotFoundPage.tsx

+ 14 - 4
packages/app/src/components/NotFoundPage.tsx

@@ -1,4 +1,4 @@
-import React, { useMemo } from 'react';
+import React, { useMemo, useEffect } from 'react';
 
 import { useTranslation } from 'react-i18next';
 
@@ -10,15 +10,25 @@ import PageListIcon from './Icons/PageListIcon';
 import TimeLineIcon from './Icons/TimeLineIcon';
 import PageTimeline from './PageTimeline';
 
+/**
+ * Replace url in address bar with new path and query parameters
+ */
+const replaceURLHistory = (pageId, path) => {
+  if (pageId != null) {
+    const queryParameters = window.location.search;
+    window.history.replaceState(null, '', `${path}${queryParameters}`);
+  }
+};
 
 const NotFoundPage = (): JSX.Element => {
   const { t } = useTranslation();
   const { data: pageId } = useCurrentPageId();
   const { data: path } = useCurrentPagePath();
 
-  if (pageId != null) {
-    window.history.replaceState(null, '', path);
-  }
+  // replace url in address bar
+  useEffect(() => {
+    replaceURLHistory(pageId, path);
+  }, [pageId, path]);
 
   const navTabMapping = useMemo(() => {
     return {