state的简写方式
<script type="text/babel">
class Weather extends React.Component {
// constructor(props) {
// super(props)
// this.state = {
// isHot:false,
// wind:"威风"
// }
// this.changeWeather = this.changeWeather.bind(this)
// }
//初始化状态
state = {
isHot:false,
wind:"威风"
}
render(){
const {isHot} = this.state;
return <h1 id="title" onClick={this.changeWeather}>今天天气很{isHot ? "炎热" : "凉爽"}</h1>
}
// changeWeather(){
// const isHot = this.state.isHot;
// this.setState({isHot:!isHot})
// }
// 自定义方法----要用赋值语句的形式+箭头函数
changeWeather = ()=>{
const isHot = this.state.isHot;
this.setState({isHot:!isHot})
}
}
ReactDOM.render(<Weather/>,document.getElementById("test"))
</script>