Skip to content

Commit

Permalink
Only pass through children prop if there are children (#4756)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp authored Sep 14, 2022
1 parent 098c767 commit c271ed3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/spotty-berries-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/react': patch
---

Only pass through children prop if there are children
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { cloneElement } from 'react';

const ClonedWithProps = (element) => (props) =>
cloneElement(element, props);

export default ClonedWithProps(<div id="cloned">Cloned With Props</div>);
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import PropsSpread from '../components/PropsSpread.jsx';
import {Research2} from '../components/Research.jsx';
import Pure from '../components/Pure.jsx';
import TypeScriptComponent from '../components/TypeScriptComponent';
import CloneElement from '../components/CloneElement';
const someProps = {
text: 'Hello world!',
Expand All @@ -29,5 +30,6 @@ const someProps = {
<Research2 client:idle />
<TypeScriptComponent client:load />
<Pure />
<CloneElement />
</body>
</html>
6 changes: 6 additions & 0 deletions packages/astro/test/react-component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ describe('React Components', () => {
expect($('#client #lazy')).to.have.lengthOf(1);
expect($('#server #lazy')).to.have.lengthOf(1);
});

it('Can pass through props with cloneElement', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerioLoad(html);
expect($('#cloned').text()).to.equal('Cloned With Props');
});
});

if (isWindows) return;
Expand Down
4 changes: 3 additions & 1 deletion packages/integrations/react/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ async function renderToStaticMarkup(Component, props, { default: children, ...sl
const newProps = {
...props,
...slots,
children: children != null ? React.createElement(StaticHtml, { value: children }) : undefined,
};
if(children != null) {
newProps.children = React.createElement(StaticHtml, { value: children });
}
const vnode = React.createElement(Component, newProps);
let html;
if (metadata && metadata.hydrate) {
Expand Down

0 comments on commit c271ed3

Please sign in to comment.