50 lines
1.5 KiB
HTML
50 lines
1.5 KiB
HTML
<!--
|
|
* @Author: chenxuan
|
|
* @Date: 2022-01-02 22:09:19
|
|
* @LastEditTime: 2022-05-02 12:18:45
|
|
* @LastEditors: chenxuan
|
|
-->
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>回调函数形式的refs属性</title>
|
|
<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>
|
|
</head>
|
|
<body>
|
|
<div id="app"></div>
|
|
<script type="text/babel">
|
|
class Demo extends React.Component{
|
|
state={ishot:true}
|
|
// 这种写法,更新时不会调用
|
|
haha=(c)=>{
|
|
this.hehe=c;
|
|
|
|
}
|
|
change=()=>{
|
|
const {ishot}=this.state;
|
|
this.setState({ishot:!ishot});
|
|
console.log(this.hehe.value)
|
|
}
|
|
render(){
|
|
const {ishot}=this.state;
|
|
return(
|
|
<div>
|
|
<li>今天天气很{this.state.ishot ? '热':'凉快'}</li>
|
|
<input type="text" ref={this.haha}/>
|
|
<button onClick={this.change}>更新</button>
|
|
</div>
|
|
)
|
|
}
|
|
}
|
|
ReactDOM.render(
|
|
<Demo/>,document.getElementById('app')
|
|
)
|
|
</script>
|
|
|
|
</body>
|
|
</html> |