Skip to content

Commit

Permalink
Fix: don't refresh main component on first page load
Browse files Browse the repository at this point in the history
  • Loading branch information
katspaugh committed May 12, 2023
1 parent 460054c commit 895627a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
23 changes: 23 additions & 0 deletions src/hooks/useRouterQueryChanged.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useRouter } from 'next/router'
import { useEffect, useState } from 'react'

// Return a given query param only when it has changed from a previous non-falsey value
const useRouterQueryChanged = (queryParam: string): string => {
const router = useRouter()
const currentQuery = router.query[queryParam]?.toString() || ''
const [_, setPrevQuery] = useState<string>(currentQuery)
const [newQuery, setNewQuery] = useState<string>('')

useEffect(() => {
setPrevQuery((prev) => {
if (prev) {
setNewQuery(currentQuery)
}
return currentQuery
})
}, [currentQuery])

return newQuery
}

export default useRouterQueryChanged
7 changes: 5 additions & 2 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const TermsBanner = dynamic(() => import('@/components/common/TermsBanner'), { s

import useSafeMessageNotifications from '@/hooks/useSafeMessageNotifications'
import useSafeMessagePendingStatuses from '@/hooks/useSafeMessagePendingStatuses'
import useRouterQueryChanged from '@/hooks/useRouterQueryChanged'

const GATEWAY_URL = IS_PRODUCTION || cgwDebugStorage.get() ? GATEWAY_URL_PRODUCTION : GATEWAY_URL_STAGING

Expand Down Expand Up @@ -94,10 +95,12 @@ const WebCoreApp = ({
router,
emotionCache = clientSideEmotionCache,
}: WebCoreAppProps): ReactElement => {
const safeKey = useRouterQueryChanged('safe')

return (
<StoreHydrator>
<Head>
<title key="default-title">{'Safe{Wallet}'}</title>
<title key="default-title">{'Safe'}</title>
<MetaTags prefetchUrl={GATEWAY_URL} />
</Head>

Expand All @@ -108,7 +111,7 @@ const WebCoreApp = ({
<InitApp />

<PageLayout pathname={router.pathname}>
<Component {...pageProps} key={router.query.safe?.toString()} />
<Component {...pageProps} key={safeKey} />
</PageLayout>

<CookieBanner />
Expand Down

0 comments on commit 895627a

Please sign in to comment.