diff options
author | 2025-04-22 13:05:13 +0200 | |
---|---|---|
committer | 2025-04-22 12:05:13 +0100 | |
commit | a19a185efd75334f2f417b433fcfaa0017fe41ee (patch) | |
tree | 853831aa7f68a8e0ee1aab0b231ba5d22b42c61c /packages/integrations/react/src/static-html.ts | |
parent | 620d15d8483dfb1822cd47833bc1653e0b704ccb (diff) | |
download | astro-a19a185efd75334f2f417b433fcfaa0017fe41ee.tar.gz astro-a19a185efd75334f2f417b433fcfaa0017fe41ee.tar.zst astro-a19a185efd75334f2f417b433fcfaa0017fe41ee.zip |
feat: convert integrations to TS (#13663)
Diffstat (limited to 'packages/integrations/react/src/static-html.ts')
-rw-r--r-- | packages/integrations/react/src/static-html.ts | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/packages/integrations/react/src/static-html.ts b/packages/integrations/react/src/static-html.ts new file mode 100644 index 000000000..010896196 --- /dev/null +++ b/packages/integrations/react/src/static-html.ts @@ -0,0 +1,33 @@ +import { createElement as h } from 'react'; + +/** + * Astro passes `children` as a string of HTML, so we need + * a wrapper `div` to render that content as VNodes. + * + * As a bonus, we can signal to React that this subtree is + * entirely static and will never change via `shouldComponentUpdate`. + */ +const StaticHtml = ({ + value, + name, + hydrate = true, +}: { value: string | null; name?: string; hydrate?: boolean }) => { + if (!value) return null; + const tagName = hydrate ? 'astro-slot' : 'astro-static-slot'; + return h(tagName, { + name, + suppressHydrationWarning: true, + dangerouslySetInnerHTML: { __html: value }, + }); +}; + +/** + * This tells React to opt-out of re-rendering this subtree, + * In addition to being a performance optimization, + * this also allows other frameworks to attach to `children`. + * + * See https://preactjs.com/guide/v8/external-dom-mutations + */ +StaticHtml.shouldComponentUpdate = () => false; + +export default StaticHtml; |