最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why ReactJS ReactDom.render() doesn't render HTML tags? - Stack Overflow

programmeradmin1浏览0评论

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">

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">
Share Improve this question edited Mar 6, 2017 at 9:25 Lucius 2,8824 gold badges23 silver badges46 bronze badges asked Mar 6, 2017 at 8:43 Daksh GuptaDaksh Gupta 7,8442 gold badges28 silver badges36 bronze badges 3
  • 1 <h1> is not valid JavaScript, it's JSX. So in order to run JSX, you need a piler. – jiyinyiyong Commented Mar 6, 2017 at 9:10
  • Babel is included – Daksh Gupta Commented Mar 6, 2017 at 9:15
  • Works after adding <script type = "text/babel"> – Daksh Gupta Commented Mar 6, 2017 at 9:17
Add a ment  | 

1 Answer 1

Reset to default 9

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

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

<div id='root'/>

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

发布评论

评论列表(0)

  1. 暂无评论