Remove useEffect flicker from ClientDate
Now that both server and client use the same locale from context, the post-hydration useEffect that re-formatted with the browser's OS locale was causing a visible flicker. Just render with the context locale directly. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
38ada3bd4f
commit
4403e22006
1 changed files with 4 additions and 12 deletions
|
|
@ -1,20 +1,12 @@
|
|||
import { useState, useEffect } from "react";
|
||||
import { useLocale } from "./LocaleContext";
|
||||
|
||||
/**
|
||||
* Renders a date using the server-detected locale for the initial render
|
||||
* (matching SSR output), then updates to the browser's native locale
|
||||
* after hydration.
|
||||
* Renders a date formatted with the server-detected locale,
|
||||
* ensuring SSR and client output match (no hydration flicker).
|
||||
*/
|
||||
export function ClientDate({ iso }: { iso: string }) {
|
||||
const locale = useLocale();
|
||||
const [formatted, setFormatted] = useState(
|
||||
new Date(iso).toLocaleDateString(locale),
|
||||
return (
|
||||
<time dateTime={iso}>{new Date(iso).toLocaleDateString(locale)}</time>
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
setFormatted(new Date(iso).toLocaleDateString());
|
||||
}, [iso]);
|
||||
|
||||
return <time dateTime={iso}>{formatted}</time>;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue