Search code examples
javascriptruby-on-railsrubyruby-on-rails-4reactjs

Uncaught ReferenceError: ReactDOM is not defined


So i have Rails applications, i installed react-rails gem, set it up and try to run test application.

Freshly installed, when i tryed to run hello world program, this error hapened:

Uncaught ReferenceError: ReactDOM is not defined

This is my react:

var HelloWorld = React.createClass({
  render: function() {
    return (
      <p>
        Hello, <input type="text" placeholder="Your name here" />!
        It is {this.props.date.toTimeString()}
      </p>
    );
  }
});

setInterval(function() {
  ReactDOM.render(
    <HelloWorld date={new Date()} />,
    document.getElementById('example')
  );
}, 500);

Its saved inside /app/assets/javascripts/components/test.js.jsx file.

Rails 4.2.4 With Ruby 2.2.3


Solution

  • ReactDOM available since version 0.14.0, so you need to use React.render (because you have a React version 0.13.3) instead,

    setInterval(function() {
      React.render(
        <HelloWorld date={new Date()} />,
        document.getElementById('example')
      );
    }, 500);
    

    or upgrade your React version and include ReactDOM

    Changes in React 0.14