import React , {Component } from 'react';
export default class center extends Component{
constructor(prop){
super(prop)
this.state={isHot:false};
this.changeWeather = this.changeWeather.bind(this)
}
render(){
let {isHot} = this.state;
return(
<div>
<div >
<h2 onClick={this.changeWeather}>今天天气真{isHot?'炎热':'凉爽'}</h2>
</div>
</div>
)
}
// 点击事件
changeWeather(){
let {isHot} = this.state;
this.setState({isHot:!isHot})
}
}
下面是精简版
import React , {Component } from 'react';
export default class center extends Component{
state={isHot:false};
render(){
let {isHot} = this.state;
return(
<div>
<div >
<h2 onClick={this.changeWeather}>今天天气真{isHot?'炎热':'凉爽'}</h2>
</div>
</div>
)
}
// 点击事件
changeWeather=()=>{
let {isHot} = this.state;
this.setState({isHot:!isHot})
}
}