-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing the following error: > Hydration failed because the initial UI does not match what was rendered on the server. This was due to the startTime being different on the server than on the client since on the server it is in Eastern time but on the client it uses the users local time zone (which may be different). This is fixed by using a `useHydration` hook with a Suspense boundary. More info here: https://francoisbest.com/posts/2023/displaying-local-times-in-nextjs
- Loading branch information
Showing
3 changed files
with
43 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Suspense, useEffect, useState } from "react"; | ||
import { useHydration } from "~/hooks/useHydration"; | ||
|
||
export type StartTimeProps = { | ||
readonly date: Date; | ||
}; | ||
|
||
const useLocales = (): string[] => { | ||
const [locales, setLocales] = useState(["en"]); | ||
|
||
useEffect(() => { | ||
setLocales([...window.navigator.languages]); | ||
}, []); | ||
|
||
return locales; | ||
}; | ||
|
||
export const StartTime = ({ date }: StartTimeProps) => { | ||
const iso = date.toISOString(); | ||
const hydrated = useHydration(); | ||
const locales = useLocales(); | ||
|
||
return ( | ||
<Suspense key={hydrated ? "local" : "utc"}> | ||
<time dateTime={iso} title={iso}> | ||
{new Intl.DateTimeFormat(locales, { timeStyle: "short" }).format(date)} | ||
</time> | ||
</Suspense> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
export const useHydration = (): boolean => { | ||
const [hydrated, setHydrated] = useState(false); | ||
|
||
useEffect(() => { | ||
setHydrated(true); | ||
}, []); | ||
|
||
return hydrated; | ||
}; |
40c3932
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
nhl-remix – ./
nhl-remix-smoak.vercel.app
nhl-remix-git-main-smoak.vercel.app
nhl-remix.vercel.app