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

🐛 BUG: React component hydration error when loaded with client:only #3010

Closed
1 task done
jhuleatt opened this issue Apr 6, 2022 · 12 comments · Fixed by #3337
Closed
1 task done

🐛 BUG: React component hydration error when loaded with client:only #3010

jhuleatt opened this issue Apr 6, 2022 · 12 comments · Fixed by #3337
Assignees
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)

Comments

@jhuleatt
Copy link
Contributor

jhuleatt commented Apr 6, 2022

What version of astro are you using?

1.0.0-beta.2

What package manager are you using?

npm

What operating system are you using?

Mac

Describe the Bug

@astrojs/react: 0.1.0

React components loaded with client:only show a hydration error:

Screen Shot 2022-04-06 at 3 06 20 PM

The component still works, it just makes React unhappy. client:only skips the component at build time, so there's nothing to hydrate. React components rendered withclient:only should probably use createRoot instead of hydrateRoot:

Though in looking at the code, there is a suppressHydrationWarning flag - is that related?:

{ ...props, suppressHydrationWarning: true },

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-2hpcon

Participation

  • I am willing to submit a pull request for this issue.
@matthewp
Copy link
Contributor

Thanks! can you submit a PR for this?

@tony-sull
Copy link
Contributor

Looks like this also causes a similar warning in Vue,

• [Vue warn]: Attempting to hydrate existing markup but container is empty. Performing full mount instead.

@pmochine
Copy link

I get the same warning @matthewp with vue, but I cannot PR it though 🙈.
Here is a link with the warning: https://stackblitz.com/edit/github-rfuoqw?file=src%2Fcomponents%2FSwiperWrapper.vue&on=stackblitz (Have a check in the console after vite has connected)

@JLarky
Copy link
Contributor

JLarky commented May 2, 2022

I managed to reproduce the issue https://youtu.be/kSgRvK0xWLw?t=6965

The solution is to remove suppressHydrationWarning from packages/integration/react/client.js and use createRoot or hydrateRoot depending if the element has existing HTML (that was rendered on the server) in it or not

@bholmesdev bholmesdev added - P3: minor bug An edge case that only affects very specific usage (priority) s1-small and removed bb:investigate labels May 10, 2022
@bholmesdev
Copy link
Contributor

@JLarky Dang, thanks for the detailed livestream there! Almost feels unfair making the PR myself here since you explained it so well... definitely feel free to contribute in the future, but I have a PR coming in 👍

@bholmesdev bholmesdev self-assigned this May 10, 2022
@JLarky
Copy link
Contributor

JLarky commented May 10, 2022

@JLarky Dang, thanks for the detailed livestream there! Almost feels unfair making the PR myself here since you explained it so well... definitely feel free to contribute in the future, but I have a PR coming in 👍

haha, thanks :) I would definitely help with the code if I had more time :) but I appreciate the kind words about the video :)

@bholmesdev
Copy link
Contributor

@JLarky Well, I spoke too soon there. We actually avoid using render and createRoot over hydrateRoot by design. Using createRoot will blow away any server-rendered children passed to a client:only component. This is why we hydrate with hydration suppression warnings today. We'll need to investigate more fundamental changes for this.

@JLarky
Copy link
Contributor

JLarky commented May 11, 2022

right, that's what I was talking about :) you have to intelligently use one or the other, otherwise, it will either be not working (doing hydrateRoot when no HTML is there in client:only instead of createRoot) or show warnings (trying to re-hydrate existing HTML with createRoot instead of hydrateRoot).

@bholmesdev
Copy link
Contributor

@JLarky Correct, that's what I implemented! I just got some conflicting feedback from the team about whether createRoot will work with our bundling and hydration strategy. Hate to say it's much different than standard React children. Checking that this is a safe change to make 👍

@CreativeSight
Copy link

CreativeSight commented Jun 5, 2024

Hey guys issue is back?
Astro 4.9.3
React 18.3.1
React Hook Form 7.51.5

Static exported build got same error with hydration in two different projects
and error is explained here -> https://react.dev/errors/418?invariant=418

Screenshot 2024-06-05 at 15 36 27

@julien-e
Copy link

Hello,

Same error

Astro 4.12.1
React 18.3.1

@ematipico
Copy link
Member

@julien-e open a new issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants