一.组件名称首字母大写、组件类名称首字母大写
二.有constructor一定要有super
子类必须在constructor方法中调用super方法,否则新建实例时会报错。这是因为子类没有自己的this对象,而是继承父类的this对象,然后对其进行加工。如果不调用super方法,子类就得不到this对象
constructor(props){
//有constructor必须有super,super用于类的继承,子类得到this
super(props)
//react定义数据
this.state={
msg:'我是一个home组件',
username:''
}
}
三.render()中可以直接写模板元素,不需要使用ES6模板字符串,当有多行元素时return加一个().
四.render()中return里面只能有一个子节点。
五.绑定数据时使用{ }
六.绑定属性注意:
-
class 要变成 className
-
for 要变成 htmlFor
-
style属性和以前的写法有些不一样
<div style={{'color':'blue'}}>{this.state.title}</div> <div style={{'color':this.state.color}}>{this.state.title}</div>