53 lines
1.2 KiB
HTML
53 lines
1.2 KiB
HTML
<!--
|
|
* @Author: chenxuan
|
|
* @Date: 2022-01-08 20:22:21
|
|
* @LastEditTime: 2022-01-09 17:13:49
|
|
* @LastEditors: chenxuan
|
|
-->
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>生命周期函数</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 Life extends React.Component{
|
|
state={opacity:1}
|
|
death=()=>{
|
|
// clearInterval(this.timer);
|
|
ReactDOM.unmountComponentAtNode(document.getElementById('app'))
|
|
}
|
|
componentDidMount(){
|
|
this.timer=setInterval(()=>{
|
|
let {opacity}=this.state;
|
|
opacity-=0.1;
|
|
if(opacity<=0){
|
|
opacity=1
|
|
}
|
|
this.setState({opacity})
|
|
},200)
|
|
}
|
|
componentWillUnmount(){
|
|
clearInterval(this.timer);
|
|
}
|
|
render(){
|
|
return(
|
|
<div>
|
|
<h2 style={{opacity:this.state.opacity}}>React学不会怎么办</h2>
|
|
<button onClick={this.death}>销毁</button>
|
|
</div>
|
|
)
|
|
}
|
|
}
|
|
ReactDOM.render(
|
|
<Life/>,document.getElementById('app')
|
|
)
|
|
</script>
|
|
</body>
|
|
</html> |