export default function NoFirstRender({ theDate }) {
const [hydrated, setHydrated] = React.useState(false);
React.useEffect(() => {
// This forces a rerender, so the date is rendered
// the second time but not the first
setHydrated(true);
}, []);
if (!hydrated) {
// Returns null on first render, so the client and server match
return null;
}
const formatted_date = new Date(theDate).toLocaleDateString();
return <div>{formatted_date}</div>;
}
Created
January 5, 2023 08:27
-
-
Save amit08255/de985b817de7384aae3417f6c4174389 to your computer and use it in GitHub Desktop.
Fix React Hydration Errors
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment