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

experimentalReactChildren flag has issues when passing Image elements #9833

Closed
1 task done
StandardGage opened this issue Jan 26, 2024 · 1 comment · Fixed by #9849
Closed
1 task done

experimentalReactChildren flag has issues when passing Image elements #9833

StandardGage opened this issue Jan 26, 2024 · 1 comment · Fixed by #9849
Labels
- P2: nice to have Not breaking anything but nice to have (priority) good first issue Good for newcomers. If you need additional guidance, feel free to post in #dev on Discord pkg: react Related to React (scope)

Comments

@StandardGage
Copy link
Contributor

StandardGage commented Jan 26, 2024

Astro Info

Astro                    v4.1.2
Node                     v20.10.0
System                   Windows (x64)
Package Manager          npm
Output                   hybrid
Adapter                  @astrojs/vercel/serverless
Integrations             @astrojs/react
                         @astrojs/tailwind

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

When passing any sort of image to a React component (such as Astro's Image component or a simple <img> element), specifically when the client directive is used, the image will appear for a split second before disappearing. Looking at the console shows an error:
Warning: An error occurred during hydration. The server HTML was replaced with client content in <astro-island>. So it seems Astro is replacing the content. The component works perfectly if hydration is not used, but this kind of defeats the purpose of using react here.

My example below shows multiple different image variations that I tried to pass as children to a React component.

What's the expected result?

The Image should be handed off to React without issues as if it was any other child.

Link to Minimal Reproducible Example

sandbox

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Jan 26, 2024
@lilnasy lilnasy added pkg: react Related to React (scope) - P2: nice to have Not breaking anything but nice to have (priority) and removed needs triage Issue needs to be triaged labels Jan 26, 2024
@lilnasy
Copy link
Contributor

lilnasy commented Jan 26, 2024

Good first issue!

This is an opportunity for astro users to learn about how astro works by fixing a small but real bug. You can expect to get help setting up the dev environment, and understanding the project structure in #dev channel of our discord server (https://astro.build/chat).

What's the bug?

Presence of void elements (img, source, br) in slotted children of a react component breaks hydration with experimentalReactChildren enabled.

What's going wrong here?

When experimentalReactChildren is enabled, the react integration's client-side code parses the slotted html to turn it into virtual nodes. It always creates an array as props.children of the virtual nodes it creates, even when there are 0 children elements. React breaks when it finds an empty array as props.children of an img tag.

Where do I start looking?

The part where the the react integration's client-side code generates virtual DOM nodes using html strings is here:

if (experimentalReactChildren && childString) {
let children = [];
let template = document.createElement('template');
template.innerHTML = childString;
for (let child of template.content.children) {
children.push(createReactElementFromDOMElement(child));
}
return children;

A similar fix was made for the server-side code in #9141.

@florian-lefebvre florian-lefebvre added the good first issue Good for newcomers. If you need additional guidance, feel free to post in #dev on Discord label Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P2: nice to have Not breaking anything but nice to have (priority) good first issue Good for newcomers. If you need additional guidance, feel free to post in #dev on Discord pkg: react Related to React (scope)
Projects
None yet
3 participants