<script src="../../build/react.js"></script> <script src="../../build/react-dom.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.min.js"></script> <script type="text/babel"> var ExampleApplication = React.createClass({ render: function() { var elapsed = Math.round(this.props.elapsed / 100); var seconds = elapsed / 10 + (elapsed % 10 ? '' : '.0' ); var message = 'React has been successfully running for ' + seconds + ' seconds.'; return <p>{message}</p>; } }); var start = new Date().getTime(); setInterval(function() { ReactDOM.render( <ExampleApplication elapsed={new Date().getTime() - start} />, document.getElementById('container') ); }, 50); </script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.min.js"></script>
注意这个babel文件非常大,请问必须输出到页面吗?
react的代码的JSX写的,浏览器是无法识别JSX代码的,babel文件是将JSX转化为JS,也能将es6的代码转化为es5,在实际项目中,会通过webpack或者gulp等工具,将jsx的代码转化为js代码,就不需要引babel文件了。