Files
react/basic/6/P06.html
T
2024-04-12 09:58:34 +08:00

70 lines
2.3 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!--
* @Author: chenxuan
* @Date: 2022-01-13 22:59:55
* @LastEditTime: 2022-01-13 23:14:43
* @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>getSnapshotBeforeUpdate</title>
<script src="../package/new/react.development.js"></script>
<script src="../package/new/react-dom.development.js"></script>
<script src="../package/new/babel.min.js"></script>
</head>
<body>
<div id="app"></div>
<script type="text/babel">
class Count extends React.Component{
constructor(props){
console.log("1-constructor")
super(props)
this.state={count:100}
}
add=()=>{
let {count}=this.state;
count+=1;
this.setState({count})
}
render(){
console.log("3-render")
return (
<div>
<h2>当前求和为:{this.state.count}</h2>
<button onClick={this.add}>点我+1</button>
</div>
)
}
componentDidMount(){
console.log('4-componentDidMount')
}
componentWillUnmount(){
console.log("5-componentWillUnmount")
}
// 控制组件更新的阀门
shouldComponentUpdate(){
console.log('shouldComponentUpdate')
return true
}
// 组件更新之前搞一些事情
// 次钩子函数的任何值都会传递给componentDidUpdate
getSnapshotBeforeUpdate(prevProps,prevState){
// console.log(preState)
return "getSnapshotBeforeUpdate"
}
componentDidUpdate(preProps,preState,snapshotValue){
console.log(snapshotValue)
// console.log(preProps,preState)
// console.log('componentDidUpdate')
}
}
ReactDOM.render(
<Count count={1999} />,document.getElementById('app')
)
</script>
</body>
</html>