React can be used on server side rendering. What does that mean? - reactjs

I am new to React and Redux . Although i know that React is just a view layer . But i saw a term "React can be used on server side rendering". What does this mean and how it works behind the scene with nodejs.Can anyone help me in getting clear the fact that "What is server side render in react".

The react-dom package includes a server module. This module allows you render your react application to a simple HTML string with reactDOMServer.renderTostring(). Basically a snapshot of your view for a given set of props:
https://facebook.github.io/react/docs/top-level-api.html#reactdomserver.rendertostring
Additionally this functions calculates a unique hash from the string it generated and appends it to the html. On the client side react can "pick up" the server generated HTML and use it as its "first render".
Though the client side first render and the server render need to produce the exact same output for react to pick it up on the client side. React can check this via the generated hash: The client side react will also generate a html string (without modifying the actual DOM, I think this is done only on the virtual DOM). Now it can also calculate a hash from its virtual DOM and compare it with the server rendered one. If they match, no rendering needs be done. If they don't, the client side react will throw away the server generated DOM and replace it with its version (and print out an error/warning).
In combination with redux this means in addition to rendering the HTML you need to pass down the state of your store (store.getState()) to the client. The client can then use this serialized state as an initial state when creating its instance of the redux store. This will lead to both renders (client + server) to match up.
If you don't need the client side to do anything and just want to create static markup on the server side, react-dom offers a renderToStaticMarkup() function:
https://facebook.github.io/react/docs/top-level-api.html#reactdomserver.rendertostaticmarkup

Related

React both client side and server side rendering

I am working on one react application.
My requirements are :-
1) First two pages should be rendered always from server side.
2) Rest pages should be client side rendered.
For example :-
http://foo.com and http://foo.com/about I want to rendered always from server side.
http://foo.com/FAQ, http://foo.com/contact I want to render from client side.
what is the right way to achieve this?
You should use ReactDOMServer and specificly renderToString() method.
ReactDOMServer.renderToString(element)
Render a React element to its
initial HTML. This should only be used on the server. React will
return an HTML string. You can use this method to generate HTML on the
server and send the markup down on the initial request for faster page
loads and to allow search engines to crawl your pages for SEO
purposes.
If you call ReactDOM.render() on a node that already has this
server-rendered markup, React will preserve it and only attach event
handlers, allowing you to have a very performant first-load
experience.

React Redux Server side rendered rerendering on client again

I created server side rendering with approach described in redux's official site, everything is great, but on client side it rendering components again, and this is not good I think. I am passing same state from server to window.__STATE__ variable and passing this to my client side createStore method as initial state, but it rerendering again.
Also please write in comments which part of code is needed to you, if so.
I am not providing since it is very similar to official page instructions code and there is no errors, just issue with rerendering, but as I understand it is not connecting to virtual DOM.
Please help me find valid way for handling this task.
Take a look at this example from the ReactGo project: https://github.com/reactGo/reactGo/blob/master/app/client.jsx#L22
They use a function onUpdate that has the conditional
if (window.__INITIAL_STATE__ !== null) {
window.__INITIAL_STATE__ = null;
return;
}
which prevents a duplicate fetches if __INITIAL_STATE__ is already defined. Your components rerendering may have something to do with the duplicate fetching.
Perhaps I am not understanding what you mean by re-rendering, but it is supposed to "re-render" on the client again. The way isomorphic works is that it renders the HTML on the server, and then the payload includes the initial state as well has the HTML markup - this way the browser "appears" to have faster page load times - since the UI is rendered even before the script is executed. Now once the HTML parsed and the script runs, React internally builds the virtual DOM and then compares it to the server generated DOM and wires up event listeners etc. It does not however, do a full re-render in that no new DOM elements should be created. If for any reason the client run of your React render results in a virtual DOM that is different from the generated server DOM, React will give you a warning.
"Warning: React attempted to reuse markup in a container but the
checksum was invalid. This generally means that you are using server
rendering and the markup generated on the server was not what the
client was expecting. React injected new markup to compensate which
works but you have lost many of the benefits of server rendering.
Instead, figure out why the markup being generated is different on the
client or server:"

React checksum fails when using server side rendering

I build an isomorphic react app and some time a meta tag is not self closing on the server side, and i have the following invalid checksum error :
You're trying to render a component to the document using server rendering but the checksum was invalid. This usually means you rendered a different component type or props on the client from the one on the server, or your render() methods are impure. React cannot handle this case due to cross-browser quirks by rendering at the document root. You should look for environment dependent code in your components and ensure the props are the same client and server side:
(client) -8" data-reactid="3"/><meta http-equiv="
(server) -8" data-reactid="3"><meta http-equiv="X
Is there a way to avoid this? any ideas ?
Assuming the keys are identical (which they seem to be) this was a known issue in the 0.13.* versions. Ensuring both react and react-dom packages are both at least 0.14.0 you should be good.

ReactJS: checksum was invalid

I'm receiving this error, because server renders version of the webapp for not-authenticated users, which means some of UI elements are absent. Client side retrieves locally stored user token, then automatic authentication happens and client side renders slightly different DOM. The logic is ok, right? But how do i make this error stop popping up?
React attempted to reuse markup in a container but the checksum was invalid. This generally means that you are using server rendering and the markup generated on the server was not what the client was expecting. React injected new markup to compensate which works but you have lost many of the benefits of server rendering. Instead, figure out why the markup being generated is different on the client or server:
(client) d=".c4gd6urcw.2.0"><noscript data-reacti
(server) d=".c4gd6urcw.2.0"><div data-reactid=".c
You could use cookies to store the token so the server can read them and handle the authentication there. Or, if you're set on keeping it in LocalStorage, as long as you trigger a secondary render after the initial that handles the login it should satisfy React. The warning is that it has to rerender your entire app since the checksums don't match. Maybe something like this?
componentDidMount() {
// this is only called client side after the server render
if (localStorageHasToken) {
handleAuth()
}
}
The goal is just to make that initial client render match what the server sent it initially. Throwing the authentication in a new process should ensure the initial checksums match and then the rest is just basic React rerendering.

What is checksum in React and how to use it?

I'm reading this example of React server-side rendering. It states:
Try viewing the page source to ensure the HTML being sent from the server is already rendered (with checksums to determine whether client-side rendering is necessary)
Ok, I saw page source and there is indeed a data-react-checksum attribute:
<div data-reactid=".157rq30hudc" data-react-checksum="556954499">
And when I check the element in a browser console, it also has the attribute:
Then I decided to checked my site that uses React server side rendering too. And I see strange thing. Page source has data-react-checksum attribute but the element is a console doesn't.
Page Source:
<div class="activityOptionBox" data-reactid=".1l6uko4wt8g" data-react-checksum="168103090">
Console:
What does it mean? What for checksum and how to read/use it?
The checksum is used internally by React on the client, when using server rendering via ReactDOM.renderToString, to determine if the output from the server matches the output from the client. If it does, React can transparently upgrade the existing DOM from the server into a client-side app very efficiently. If it doesn't match, it means that the state and props to render on the server were different from the state and props to render on the client, and React must do a more expensive operation to initialize the client-side app.
I checked an app of mine, and it seems that—in at least some versions of React—the checksum attribute is removed from the element once the client-side application boots, though it is visible in the source code of the page.
Since the checksum is an internal consistency mechanism, you don't need to worry about it unless React detects that your server checksum and client checksum don't match, in which case it'll tell you via a warning or error in the console.

Resources