When running this code with node server.js:
var React = require('react');
var HelloMessage = React.createClass({displayName: "HelloMessage",
render: function() {
return React.createElement("div", null, "Hello ", this.props.name);
}
});
var markup = React.renderComponentToString(HelloMessage({name: "John"}));
console.log(markup);
I get the following error:
/Users/john/Sites/test/server.js:9
return React.createElement("div", null, "Hello ", this.props.name);
^
TypeError: Object #<Object> has no method 'createElement'
I think it's not working because the DOM is not available on the server, which makes sense.
How can I render/compile a ReactJS component to an HTML string in Node?
Thanks
reactdo you have installed? That exact code prints<div data-reactid=".9zpzkvv9c0" data-react-checksum="-1155126098"><span data-reactid=".9zpzkvv9c0.0">Hello </span><span data-reactid=".9zpzkvv9c0.1">John</span></div>for me.