static-html.js 884 B

1234567891011121314151617181920212223242526272829
  1. import { createElement as h } from 'react';
  2. /**
  3. * Astro passes `children` as a string of HTML, so we need
  4. * a wrapper `div` to render that content as VNodes.
  5. *
  6. * As a bonus, we can signal to React that this subtree is
  7. * entirely static and will never change via `shouldComponentUpdate`.
  8. */
  9. const StaticHtml = ({ value, name, hydrate = true }) => {
  10. if (!value) return null;
  11. const tagName = hydrate ? 'astro-slot' : 'astro-static-slot';
  12. return h(tagName, {
  13. name,
  14. suppressHydrationWarning: true,
  15. dangerouslySetInnerHTML: { __html: value },
  16. });
  17. };
  18. /**
  19. * This tells React to opt-out of re-rendering this subtree,
  20. * In addition to being a performance optimization,
  21. * this also allows other frameworks to attach to `children`.
  22. *
  23. * See https://preactjs.com/guide/v8/external-dom-mutations
  24. */
  25. StaticHtml.shouldComponentUpdate = () => false;
  26. export default StaticHtml;