React.js Serverside rendering and Event Handlers

This behaviour is because of what exactly server side rendering is.
Firstly you will have to run the exact same code both on client side and server side. This is what is called an isomorphic application. One that runs both on server and client.
So, on doing ReactDOM.renderToString(<Component>) only the HTML is rendered as a string. The render method of your component is evaluated and the HTML required for initial rendering is generated.
When the same code is run on client, react looks up the HTML rendered and attaches JS at required places. React is smart this way, it doesn’t re-render everything again at client side. Just evaluates the code and identifies where all to attach the code based on react-id each DOM element is given. (You’ll react-id if you inspect element any react app)

Now one might ask what is the benefit of rendering the same thing twice?
and the answer is perceived loading time by the user. And also some minimal viewing for users who disabled JS.

Client rendered application
This is how a solely client rendered application works. (client rendered React application too)

client rendered app

The User will only see content after all skeleton HTML, JS bundles(which are often pretty big), and data is fetched and evaluated. Which means the user will often have to stare at a spinner or loading screen for a while until everything loads.

Isomorphic application (runs both on client and server)
How an Isomorphic application works,
server rendered app
In this case the server generates the full HTML by evaluating your component. And the user will see content immediately as soon as the HTML is downloaded. Although the app will only function fully once the JS bundles are also downloaded and evaluated. So the JS has to run on both sides
Thus the user sees content much faster than before. Hence the huge decrease in perceived loading time.

Leave a Comment