Created
April 5, 2023 05:31
-
-
Save paul-phan/fc5e7b8230e1a166e5d7a89a32854f1e to your computer and use it in GitHub Desktop.
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
import React, { useId, useState } from 'react' | |
export function NoHydrate({ | |
getHTML, | |
...rest | |
}: { getHTML?: () => string } & JSX.IntrinsicElements['div']) { | |
let id = useId() | |
let [html] = useState(() => { | |
if (typeof document === 'undefined') { | |
return getHTML?.() ?? '' | |
} | |
let el = document.getElementById(id) | |
if (!el) return getHTML?.() ?? '' | |
return el.innerHTML | |
}) | |
return <div {...rest} id={id} dangerouslySetInnerHTML={{ __html: html }} /> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment