Support passing children
as props to a React component
#5886
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes
This PR fixes #5493.
Passing
children
as props to a React component, e.g.<WithChildren client:load children="test" />
would break in production with the component disappearing after hydration and React complaining with the following error:When creating the static HTML element for
children
, theprops.children
props was previously ignored. This PR introduces a change to fallback toprops.children
ifchildren
is not defined which fixes the issue.This PR also adds this change to
packages/integrations/react/server-v17.js
for the React 17 integration, altho, while adding this, I noticed that the changes introduced in #4756 were not applied to the React 17 integration so I added them as well.Testing
I updated the
react-component
fixture forastro
to include a newWithChildren
component rendering thechildren
props and updated the associated test to render this component twice, once withchildren
and once withprops.children
and assert that the rendered HTML is the same (which would fail without the changes in this PR as the<astro-slot>
was missing in the second case).I also manually tested locally this change in a local repro using the
file:
pnpm protocol and confirmed a component usingchildren
passed down as props is now properly rendered in production and the React hydration error is gone.Docs
I could not find any specific mention regarding this specific behavior in the docs and I'm not sure if it's worth mentioning it.