Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/components/Announcement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useRouter } from 'next/router'
import type { NextRouter } from 'next/router'
import { Icon } from '~/components/Icon'
import { subscribeToLocalStorage } from '~/contexts/LocalStorage'
import { useIsClient } from '~/hooks'

// change 'value' for new announcements
export const ANNOUNCEMENT = {
Expand Down Expand Up @@ -52,6 +53,8 @@ export function Announcement({
const routeAnnouncementKey = router.pathname + key
const routeAnnouncementValue = router.pathname + value

const isClient = useIsClient()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already have a useIsClient hook for this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right I’ve used useIsClient to handle the hydration check and ensure the component doesn’t render fix 08f9df2
And only the hydration check is required to fix this issue

const closeAnnouncement = () => {
localStorage.setItem(routeAnnouncementKey, JSON.stringify({ value: routeAnnouncementValue }))
window.dispatchEvent(new Event('storage'))
Expand All @@ -63,6 +66,10 @@ export function Announcement({
() => null
)

// Wait for hydration before rendering

if (!isClient) return null

if (notCancellable ? false : JSON.parse(store)?.value === routeAnnouncementValue) {
return null
}
Expand Down