4

So I have decided to start learning React.js, but am having problems rendering into the HTML

Cant seem to see whats wrong any help would be great both the HTML and JSX are below

(Note Full links to the react library are there just could post them here since Stack doesnt allow URL Shortners)

<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="react-15.2.1.js"></script>
    <script src="react-dom-15.2.1.js"></script>
    <script src="script.js"></script>
  </head>

  <body>
    <div id="root"></div>
  </body>

</html>

JSX

// Code goes here

var Button = React.createClass({
  render: function() {
    return ( < button > Go < /button>)
  }
});

React.render( < Button / > , document.getElementById("root"));
4
  • JSX isn't a part of JavaScript natively, you'll have to transpile it into regular JavaScript to be able to develop in React (unless you wanna ditch the jsx syntax). Facebook recently released a generator that should set up a basic boilerplate, so you could look into using that: github.com/facebookincubator/create-react-app Commented Jul 24, 2016 at 0:27
  • @MatisLepik But I am using Plunker to develop in and it has native React Support Commented Jul 24, 2016 at 0:32
  • 1
    Ah. In that case, replace React.render with ReactDOM.render. Also, put the <script> tags at the end of the body (after the #root element) to ensure that the DOM has loaded before the javascript starts executing. Commented Jul 24, 2016 at 0:37
  • @MatisLepik Yup that was it thanks alot Commented Jul 24, 2016 at 0:41

1 Answer 1

6

I have corrected your code, and placed the working result in this fiddle:

Your last line was wrong, it should be like this:

ReactDOM.render( < Button / > , document.getElementById("root"));
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah I know it was answered 15 mins ago in a line above you
oops, just saw that. Anyway, my answer is always with a jsfiddle :_)

Your Answer

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