Skip to content

Commit 769265b

Browse files
sanjaiyan-devflorian-lefebvresarah11918
authored
feat: Optimize StaticHtml component for React js 🚀 (#14917)
* feat: Optimize StaticHtml component for React js Refactors the StaticHtml component to use React.memo for efficient static HTML rendering, improving performance and compatibility. * Add Changset 🌟 * Update .changeset/tough-days-sell.md Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev> * Update .changeset/tough-days-sell.md More detailed changeset. Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com> --------- Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev> Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>
1 parent 016207a commit 769265b

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

.changeset/tough-days-sell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/react': patch
3+
---
4+
5+
Refactors to improve the performance of rendering static HTML content in React

packages/integrations/react/src/static-html.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createElement as h } from 'react';
1+
import { createElement as h, memo } from 'react';
22

33
/**
44
* Astro passes `children` as a string of HTML, so we need
@@ -26,12 +26,10 @@ const StaticHtml = ({
2626
};
2727

2828
/**
29-
* This tells React to opt-out of re-rendering this subtree,
30-
* In addition to being a performance optimization,
31-
* this also allows other frameworks to attach to `children`.
32-
*
33-
* See https://preactjs.com/guide/v8/external-dom-mutations
29+
* React.memo is the modern functional equivalent of shouldComponentUpdate.
30+
*
31+
* By returning `true` in the comparison function (the second argument),
32+
* we tell React that the props are "equal" and it should skip re-rendering,
33+
* effectively making this subtree static.
3434
*/
35-
StaticHtml.shouldComponentUpdate = () => false;
36-
37-
export default StaticHtml;
35+
export default memo(StaticHtml, () => true);

0 commit comments

Comments
 (0)