1: react 中 status 属性:
// 创建一个Student类, 继承Person类
class Student extends Person {
constructor (name, age, grade) {
super(name, age) // 子类重写父类构造方法, 一定要使用super 关键字, 可以将参数通过super()
传递给父类
this.grade = grade
}
// 重写父类的构造方法
speak() {
console.log(`名字${this.name}, 年龄${this.age}, 成绩${this.grade}分`)
}
}
-----------------------------------------------------------------------
// 准备一个容器
<div id="test"><div/>
初始化status 状态
<script type="text/babel">
// 1: 天气组件
class Weather extends React.Component {
constuctor(props) {
super(props);
this.state = { isHot:true }
}
render() {
console.log(this); // this 指向类组件的实例对象
let { isHot } = this.state
return <h1>今天天气很{ isHot ? "炎热": "凉爽"}</h1>
}
}
2: 把组件渲染到页面
ReactDOM.render(<Weather />, document.getElementById('test'))
</script>
----------------------------------------------------------------------
在创建类式组件中, 的render 方法中的this 就是指向类式组建的实例对象。
有状态的组件就是复杂的组件:
改变类实例的属性的时候, 可以通过构造函数。 通过构造函数传递一些参数, 让类实例的某个属性, 等于构造函数传递某个参数。