学习React时遇到的报错记录
1._registerComponent(…): Target container is not a DOM element.
错误原因:在页面中找不到对应的DOM元素,
错误代码:
<body>
<div id="app"></div> //此处的DOM元素的ID是app
</body>
<script type="text/babel">
function MailBox(props) {
const unreadMessage = props.unreadMessage;
return(
<div>
<h1>Hello!</h1>
{unreadMessage.length > 0 &&
<h2>
You have {unreadMessage.length} unread messages.
</h2>
}
</div>
)
}
const message=['React','hello','cheng','hahah'];
ReactDOM.render(
<MailBox unreadMessage={message}/>,
document.getElementById('root') //此处却在查找ID为root的DOM元素进行渲染,页面中没有对应的元素,因此报错
)
</script>