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.

This is a question.

In React 16, when using SSR, in the client-side code, we need to use hydrate instead of render so that React only registers event handler. I guess that React does this by comparing the checksum between server-side rendered html and supposed-to-be client-side rendered html.

While playing with Preact and attempting to perform SSR, I notice that at first my component was rendered twice. The rendering code is

// Server code
app.get("/", (req, res) => {
  const html = renderToString(<App />)
  res.render('index', { html })
})


// Client code
const root = document.getElementById("root")
render(<App />, root)

and the resulting html is

...
<div id="root">
  <div>This is my app.</div>
  <div>This is my app.</div>
</div>
...

When I change the client code to be

// Client code
const root = document.getElementById("root")
render(<App />, document.body, root)

The rendered html appears to be correct

...
<div id="root">
  <div>This is my app.</div>
</div>
...

but I wonder if it was the original html with preact event all hooked up, or if the content was replaced with exactly the same content.

Please help me shed some light on this.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

22reactions
developitcommented, May 17, 2018

There is no meaningful difference between hydrate() and render() within Preact, since we don’t use checksums.

If you’re seeing a flash of white or similar, check that you’re not rendering something that is initially different from your server-rendered HTML. Also make sure you’re interpreting the meaning of “parent” and “root” correctly:

const App = () => <div id="app">foo</div>

// our "server-side render":
document.write('<html><body><div id="root">' + renderToString(<App />) + '</div></body></html>')

// how "hydrate" from that:
render(<App />, document.getElementById('#root'), document.getElementById('#app'))

If you’d like a hydrate method, here’s a simple one:

function hydrate(vnode, parent) {
  return preact.render(vnode, parent, parent.firstElementChild);
}
2reactions
rattrayalex-stripecommented, May 17, 2018

Sweet. This can be closed then, correct?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding Hydration in React applications(SSR)
Hydration is used only with server-side rendering (SSR). It is a process of rendering our components and attaching event handlers.
Read more >
The Perils of Rehydration - Josh W Comeau
This is known as server-side rendering (SSR). Server-side rendering can be a ... In a rehydration, React assumes that the DOM won't change....
Read more >
Hydration and Server-side Rendering - somewhat abstract
During hydration, React works quickly in a virtual DOM to match up the existing content with what the application renders, saving time from ......
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 >
SSR Rehydration - RFC
The aim of rehydration is to avoid the client-side performance issues of a naive SSR implementation, reusing original DOM wherever possible. # ...
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