49 lines
1.4 KiB
HTML
49 lines
1.4 KiB
HTML
<!--
|
|
* @Author: chenxuan
|
|
* @Date: 2021-12-26 16:30:03
|
|
* @LastEditTime: 2022-08-13 01:46:42
|
|
* @LastEditors: chenxuan
|
|
-->
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>state</title>
|
|
</head>
|
|
<body>
|
|
<div id="app"></div>
|
|
<script src="../package/old/react.development.js"></script>
|
|
<script src="../package/old/react-dom.development.js"></script>
|
|
<script src="../package/old/babel.min.js"></script>
|
|
<script type="text/babel">
|
|
class Weather extends React.Component{
|
|
constructor(props){
|
|
super(props)
|
|
this.state={
|
|
ishot:true
|
|
}
|
|
}
|
|
render(){
|
|
const {ishot}=this.state;
|
|
// 解决this指向问题
|
|
// this.demo 这是赋值操作,此时函数并没有调用
|
|
// 点击的时候调用 demo函数 此时是直接调用,由于类的局部默认开启严格模式,this指向 undefined
|
|
// 通过 bind() 传入this 修改this指向 将this指向Weather实例
|
|
return <h1 onClick={this.demo.bind(this)}>今天天气很{ishot ? '炎热' : '凉爽'}</h1>
|
|
}
|
|
// demo定义在 Weather的原型上,供实例使用
|
|
// 通过Weather的实例调用时 this指向实例
|
|
demo(){
|
|
this.setState({
|
|
ishot:!this.state.ishot
|
|
})
|
|
}
|
|
}
|
|
ReactDOM.render(
|
|
<Weather/>,document.getElementById('app')
|
|
)
|
|
|
|
</script>
|
|
</body>
|
|
</html> |