【发布时间】:2020-06-29 02:43:22
【问题描述】:
我在 html、css 和 JS 下面有 hte HTML:
<div id="app1"></div>
JS:
function hello(props){
return(
<div className="Person">
<h1>Name: {props.name}</h1>
<p>Age: {props.age}</p>
</div>
);
}
var app=(
<div>
<hello name="John" age="82"/>
<hello name="Jane" age="89"/>
</div>
);
ReactDOM.render(app,document.querySelector("#app1"));
CSS
.Person{
width:200px;
color:black;
box-shadow:0 2px 2px #ccc;
display:inline-block;
margin:10px;
padding:2px;
}
但此代码仅在我们将组件名称为 Person 时才有效,任何其他名称都会导致以下错误
Uncaught ReferenceError: Person is not defined
at pen.js:-5
Uncaught ReferenceError: Person is not defined
at pen.js:-4
您可以假设 Reactjs 和 ReactDOM 已导入。
【问题讨论】:
-
查看reactjs.org/docs/components-and-props.html,有如下注释“React 将以小写字母开头的组件视为 DOM 标签”,因此为了使用您自己的组件,请始终以大写字母开头的组件名称。跨度>