2

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

2
  • What version of react do 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. Commented Feb 24, 2015 at 22:24
  • What a school boy error, that was exactly it. I was running 0.10.0 but after upgrading to 0.12.2 it works. Thanks! Commented Feb 24, 2015 at 23:09

1 Answer 1

1

Just needed to upgrade ReactJS to 0.12.2

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.