Skip to content

Instantly share code, notes, and snippets.

@paul-phan
Created April 5, 2023 05:31
Show Gist options
  • Save paul-phan/fc5e7b8230e1a166e5d7a89a32854f1e to your computer and use it in GitHub Desktop.
Save paul-phan/fc5e7b8230e1a166e5d7a89a32854f1e to your computer and use it in GitHub Desktop.
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