1、错误描述
react.js:20157 Uncaught Invariant Violation: _registerComponent(...): Target container is not a DOM element.
2、错误原因
<script> var ea = React.createClass({ render: function() { var elapsed = Math.round(this.props.elapsed / 100); var seconds = elapsed / 10 + (elapsed % 10 ? '' : '.0' ); var message = '开始计时:' + seconds + ' 秒'; return React.DOM.p(null,message); } }); var eaf = React.createFactory(ea); var timer = document.getElementById('timer'); var startTime = new Date().getTime(); setInterval(function() { ReactDOM.render( eaf({elapsed: new Date().getTime() - startTime}),timer ); },1000); </script>在定时器中需要获取div容器对象,这里利用变量timer来实现;但是会出现这个错误
3、解决办法
<script> var ea = React.createClass({ render: function() { var elapsed = Math.round(this.props.elapsed / 100); var seconds = elapsed / 10 + (elapsed % 10 ? '' : '.0' ); var message = '开始计时:' + seconds + ' 秒'; return React.DOM.p(null,message); } }); var eaf = React.createFactory(ea); var startTime = new Date().getTime(); setInterval(function() { ReactDOM.render( eaf({elapsed: new Date().getTime() - startTime}),document.getElementById('timer') ); },1000); </script>不利于中间变量timer,直接利用JS获取对象的方法