Pankaj Ladhar
Pankaj Ladhar

Reputation: 155

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?

Upvotes: 0

Views: 553

Answers (1)

Vlado Pandžić
Vlado Pandžić

Reputation: 5048

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.

Upvotes: 1

Related Questions