question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for hydrating portals

See original GitHub issue

Do you want to request a feature or report a bug?

Probably bug, but arguably a feature request, I suppose.

What is the current behavior?

I’ve attempted my best effort at a fiddle that shows off the particular issue. Obviously server side rendering is impossible via JSFiddle, but the markup should be equivalent to having rendered Test into a div with id test-1 during server side render.

https://jsfiddle.net/y8o5n2zg/

As seen in the fiddle, an attempt to ReactDOM.hydrate() a portal results in:

Warning: Expected server HTML to contain a matching text node for "Hello World" in <div>.

Additionally, after failing to hydrate, React renders the component and appends it resulting in a duplicated section of DOM:

<div id="test-1">Hello WorldHello World</div>

What is the expected behavior?

In an ideal world, calling hydrate on a component that has portals would allow those DOM containers to hydrate into the components they were rendered with.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

I’ve only tested this in 16.4.1, but I’ve confirmed the behavior in Chrome and Firefox. Given that I’m really looking at an edge case here I doubt it worked previously.

Why I’m doing this edge-case-y nonsense:

We’re currently using multiple React roots on our pages (as some portions of the pages are not rendered by React yet), most of which are server-side rendered. We’d like to be able to hydrate them into a single React root on page, so that we can share contexts between them without difficulty and without repeating those context components in memory (in some cases we can have a good number of roots on the page—20-30, perhaps?).

In searching, I found a few potentially related bugs (#12615, #10713, #11169), but it seemed like these really didn’t line up with my (hopefully valid?) use case.

Thanks!

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:39
  • Comments:33 (7 by maintainers)

github_iconTop GitHub Comments

18reactions
gaearoncommented, Jul 17, 2018

When you hydrate, the initial render should match your server render. But portals are currently not supported on the server. Therefore, hydrating a portal doesn’t make sense with current limitations.

I think you want to do something like:

state = {mounted: false};

componentDidMount() {
  this.setState({ mounted: true });
}

render() {
  return <div>{this.state.mounted && ReactDOM.createPortal(...)}</div>
}

Does that make sense? Same workaround as you need to use when your client render doesn’t match.

15reactions
shwaocommented, Dec 25, 2020

Still relevant

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating and hydrating self-service data lakes with AWS ...
The diagram below shows the data lake hydration process using a self-service portal. The automated hydration process using AWS Service Catalog ...
Read more >
Keeping Server-Side Rendering Cool With React Hydration
The purpose of this article is to share some helpful things to keep in mind to render a seamless experience as a Server-Side...
Read more >
11 water add-ins that make hydrating healthier | Well+Good
Wellness experts share the ways they make their water tastier and healthier, from infusing it with superfoods to stirring in skin-boosting ...
Read more >
Hydration Packs: How to Choose | REI Co-op
Which hydration pack is best? Learn about reservoir size, gear capacity and other key pack features for cyclists, runners, hikers and more.
Read more >
ReactDOM – React
Both render and hydrate have been replaced with new client methods in React 18. ... React supports all modern browsers, although some polyfills...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found