Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only pass through children prop if there are children #4756

Merged
merged 1 commit into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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