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
Solution 1:
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):
constExpress = require('express')
constReact = require('react')
constReactDomServer = require('react-dom/server')
constLabel = 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)
Post a Comment for "React V15.0.0: Since That Rendertostring Is Deprecated, How To Go About Server-side Rendering?"