feat: improve support for third-party React packages (#1701)

This commit is contained in:
Nate Moore 2021-10-29 11:23:03 -07:00 committed by GitHub
parent 6becdc8cc0
commit cb733cf689
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/renderer-react': patch
---
Improve support for third-party React packages

View file

@ -9,6 +9,12 @@ function errorIsComingFromPreactComponent(err) {
}
function check(Component, props, children) {
// Note: there are packages that do some unholy things to create "components".
// Checking the $$typeof property catches most of these patterns.
if (typeof Component === 'object') {
const $$typeof = Component['$$typeof'];
return $$typeof && $$typeof.toString().slice('Symbol('.length).startsWith('react');
}
if (typeof Component !== 'function') return false;
if (Component.prototype != null && typeof Component.prototype.render === 'function') {
@ -41,10 +47,10 @@ function check(Component, props, children) {
}
function renderToStaticMarkup(Component, props, children, metadata) {
delete props['class'];
const vnode = React.createElement(Component, {
...props,
children: React.createElement(StaticHtml, { value: children }),
innerHTML: children,
});
let html;
if (metadata && metadata.hydrate) {