Skip to content
Open
Changes from 1 commit
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
20 changes: 18 additions & 2 deletions src/components/Announcement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,34 @@ export function Announcement({
const routeAnnouncementKey = router.pathname + key
const routeAnnouncementValue = router.pathname + value

const [hydrated, setHydrated] = React.useState(false)

React.useEffect(() => {
setHydrated(true)
}, [])

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'))
}

const store = React.useSyncExternalStore(
subscribeToLocalStorage,
() => localStorage.getItem(routeAnnouncementKey) ?? null,
() => (typeof window !== 'undefined' ? localStorage.getItem(routeAnnouncementKey) ?? null : null),
() => null
)

if (notCancellable ? false : JSON.parse(store)?.value === routeAnnouncementValue) {
let parsed
try {
parsed = store ? JSON.parse(store) : null
} catch {
parsed = null
}

// Wait for hydration before rendering
if (!hydrated) return null

if (notCancellable ? false : parsed?.value === routeAnnouncementValue) {
return null
}

Expand Down