react.js:20157 Uncaught Invariant Violation: _registerComponent(...): Target container is not a DOM

前端之家收集整理的这篇文章主要介绍了react.js:20157 Uncaught Invariant Violation: _registerComponent(...): Target container is not a DOM前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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获取对象的方法 原文链接:https://www.f2er.com/react/305914.html

猜你在找的React相关文章