user3104270
user3104270

Reputation: 625

React v15.0.0: Since that renderToString is deprecated, how to go about server-side rendering?

There is a new release candidate of React, v 15.0.0. Since the renderToString method now is deprecated in the library, and apparently is going to be discontinued in future versions, what is the way to support server-side rendering with React in the new version?

On the docs page, no replacement for the renderToString or other explanation has been provided except that this particular method is no longer supported.

Thank you

Upvotes: 1

Views: 1625

Answers (1)

Pathogen
Pathogen

Reputation: 998

As described in the comments, the correct (and only) way to render to a string with recent versions of React is to use ReactDOMServer's renderToString. Lots of existing answers and documentation refer to the removed React.renderToString, though. It has been deprecated for a while, but apparently only removed recently.

A quick and dirty example of what this might look like (running with node-babel):

const Express = require('express')
const React = require('react')
const ReactDomServer = require('react-dom/server')

const Label = React.createClass({
  render: function () {
    return <p> Foo! </p>
  }
})

const server = Express()

server.use(function(req, res) {
  const appHtml = ReactDomServer.renderToString(<Label />)
  res.send(appHtml)
})

server.listen(3000)

Upvotes: 1

Related Questions