Support passing children
as props to a React component (#5886)
This commit is contained in:
parent
dabce6b8c6
commit
9d4bfc76e8
6 changed files with 27 additions and 5 deletions
5
.changeset/fresh-eels-speak.md
Normal file
5
.changeset/fresh-eels-speak.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/react': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Support passing `children` as props to a React component
|
5
packages/astro/test/fixtures/react-component/src/components/WithChildren.jsx
vendored
Normal file
5
packages/astro/test/fixtures/react-component/src/components/WithChildren.jsx
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
export default function ({ children }) {
|
||||||
|
return <div className="with-children">{children}</div>;
|
||||||
|
}
|
|
@ -7,6 +7,7 @@ import {Research2} from '../components/Research.jsx';
|
||||||
import Pure from '../components/Pure.jsx';
|
import Pure from '../components/Pure.jsx';
|
||||||
import TypeScriptComponent from '../components/TypeScriptComponent';
|
import TypeScriptComponent from '../components/TypeScriptComponent';
|
||||||
import CloneElement from '../components/CloneElement';
|
import CloneElement from '../components/CloneElement';
|
||||||
|
import WithChildren from '../components/WithChildren';
|
||||||
|
|
||||||
const someProps = {
|
const someProps = {
|
||||||
text: 'Hello world!',
|
text: 'Hello world!',
|
||||||
|
@ -31,5 +32,7 @@ const someProps = {
|
||||||
<TypeScriptComponent client:load />
|
<TypeScriptComponent client:load />
|
||||||
<Pure />
|
<Pure />
|
||||||
<CloneElement />
|
<CloneElement />
|
||||||
|
<WithChildren client:load>test</WithChildren>
|
||||||
|
<WithChildren client:load children="test" />
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -42,11 +42,16 @@ describe('React Components', () => {
|
||||||
expect($('#pure')).to.have.lengthOf(1);
|
expect($('#pure')).to.have.lengthOf(1);
|
||||||
|
|
||||||
// test 8: Check number of islands
|
// test 8: Check number of islands
|
||||||
expect($('astro-island[uid]')).to.have.lengthOf(5);
|
expect($('astro-island[uid]')).to.have.lengthOf(7);
|
||||||
|
|
||||||
// test 9: Check island deduplication
|
// test 9: Check island deduplication
|
||||||
const uniqueRootUIDs = new Set($('astro-island').map((i, el) => $(el).attr('uid')));
|
const uniqueRootUIDs = new Set($('astro-island').map((i, el) => $(el).attr('uid')));
|
||||||
expect(uniqueRootUIDs.size).to.equal(4);
|
expect(uniqueRootUIDs.size).to.equal(6);
|
||||||
|
|
||||||
|
// test 10: Should properly render children passed as props
|
||||||
|
const islandsWithChildren = $('.with-children');
|
||||||
|
expect(islandsWithChildren).to.have.lengthOf(2);
|
||||||
|
expect($(islandsWithChildren[0]).html()).to.equal($(islandsWithChildren[1]).html());
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Can load Vue', async () => {
|
it('Can load Vue', async () => {
|
||||||
|
|
|
@ -62,8 +62,11 @@ function renderToStaticMarkup(Component, props, { default: children, ...slotted
|
||||||
const newProps = {
|
const newProps = {
|
||||||
...props,
|
...props,
|
||||||
...slots,
|
...slots,
|
||||||
children: children != null ? React.createElement(StaticHtml, { value: children }) : undefined,
|
|
||||||
};
|
};
|
||||||
|
const newChildren = children ?? props.children;
|
||||||
|
if (newChildren != null) {
|
||||||
|
newProps.children = React.createElement(StaticHtml, { value: newChildren });
|
||||||
|
}
|
||||||
const vnode = React.createElement(Component, newProps);
|
const vnode = React.createElement(Component, newProps);
|
||||||
let html;
|
let html;
|
||||||
if (metadata && metadata.hydrate) {
|
if (metadata && metadata.hydrate) {
|
||||||
|
|
|
@ -69,8 +69,9 @@ async function renderToStaticMarkup(Component, props, { default: children, ...sl
|
||||||
...props,
|
...props,
|
||||||
...slots,
|
...slots,
|
||||||
};
|
};
|
||||||
if (children != null) {
|
const newChildren = children ?? props.children;
|
||||||
newProps.children = React.createElement(StaticHtml, { value: children });
|
if (newChildren != null) {
|
||||||
|
newProps.children = React.createElement(StaticHtml, { value: newChildren });
|
||||||
}
|
}
|
||||||
const vnode = React.createElement(Component, newProps);
|
const vnode = React.createElement(Component, newProps);
|
||||||
let html;
|
let html;
|
||||||
|
|
Loading…
Add table
Reference in a new issue