state
的简写方式class Car {
constructor(name, price) {
this.name = name
this.price = price
this.a = 1
}
}
const c1 = new Car('奔驰', 199)
console.log(c1)
class Car {
constructor(name, price) {
this.name = name
this.price = price
// this.a = 1
}
// 直接赋值,给 Car 的实例对象添加一个属性
a = 1
}
const c1 = new Car('奔驰', 199)
console.log(c1)
:::说明 明白了上面的写法,就知道 state
该怎么简写了 :::
state
class Person {
// 赋值语句
state = {
isMood: false
}
render() {...}
}
// 渲染组件
ReactDom.render(<Person/>, document.getElmentById('test'))
class Person {
// 赋值语句
state = {
isMood: false
}
render() {...}
// 赋值语句,只能写箭头函数,箭头函数是没有自己的 this 的他就像向外层找,就找到实例的 this
demo = () => {
const isMood = this.state.isMood
this.setState({isMood: !isMood})
}
}
// 渲染组件
ReactDom.render(<Person/>, document.getElmentById('test'))