5

why this code below doesn't work?

ReactDOM.render( <h1>Hello</h1>, document.getElementById('root'));

but this code works.

var elem = React.createElement('h1',{},"Hello");
ReactDOM.render( elem, document.getElementById('root'));

Coming back to react after long time..trying looking into the web.. didn't found it deprecated ? then why its not working?

Update: My browser console displays syntax error over here

 ReactDOM.render( <h1>Hello</h1>, document.getElementById('root'));

UPDATE: Thanks for the help everything worked by adding the following line

<script type="text/babel">
3
  • 1
    <h1> is not valid JavaScript, it's JSX. So in order to run JSX, you need a compiler. Commented Mar 6, 2017 at 9:10
  • Babel is included Commented Mar 6, 2017 at 9:15
  • Works after adding <script type = "text/babel"> Commented Mar 6, 2017 at 9:17

1 Answer 1

8

It is working, may be you are doing some other mistake, check this:

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>

<div id='root'/>

<script type="text/babel">
    ReactDOM.render( <h1>Hello</h1>, document.getElementById('root'));
</script>

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

5 Comments

Hey I have added the CDN, without CDN React.createElement(...) would not have worked...
ya, are you getting any error in console or on terminal ?
yes, Updated the browser console error in the original question
because you forgot to define the type="text/babel" check the updated answer it will work :)
I had to add the babel in website to get this to work stackoverflow.com/a/48414699/5237614

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.