xxx
This commit is contained in:
+111
@@ -0,0 +1,111 @@
|
||||
<!--
|
||||
* @Author: chenxuan
|
||||
* @Date: 2022-01-11 23:53:36
|
||||
* @LastEditTime: 2022-01-12 23:43:16
|
||||
* @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>生命周期函数</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})
|
||||
}
|
||||
death=()=>{
|
||||
ReactDOM.unmountComponentAtNode(document.getElementById('app'))
|
||||
}
|
||||
// 强制更新不受shouldComponentUpdate控制
|
||||
force=()=>{
|
||||
this.forceUpdate()
|
||||
}
|
||||
render(){
|
||||
console.log("3-render")
|
||||
return (
|
||||
<div>
|
||||
<h2>当前求和为:{this.state.count}</h2>
|
||||
<button onClick={this.add}>点我+1</button>
|
||||
<button onClick={this.death}>销毁</button>
|
||||
<button onClick={this.force}>强制更新</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
// UNSAFE_componentWillMount
|
||||
componentWillMount(){
|
||||
console.log("2-componentsWillMount")
|
||||
}
|
||||
componentDidMount(){
|
||||
console.log('4-componentDidMount')
|
||||
}
|
||||
componentWillUnmount(){
|
||||
console.log("5-componentWillUnmount")
|
||||
}
|
||||
// 控制组件更新的阀门
|
||||
shouldComponentUpdate(){
|
||||
console.log('shouldComponentUpdate')
|
||||
return true
|
||||
}
|
||||
// UNSAFE_componentWillUnmount
|
||||
componentWillUpdate(){
|
||||
console.log('componentWillUpdate')
|
||||
}
|
||||
componentDidUpdate(){
|
||||
console.log('componentDidUpdate')
|
||||
}
|
||||
}
|
||||
class A extends React.Component{
|
||||
state={carName:"奔驰"}
|
||||
changeCar=()=>{
|
||||
this.setState({carName:"哈弗"})
|
||||
}
|
||||
render(){
|
||||
return (
|
||||
<div>
|
||||
A
|
||||
<B carName={this.state.carName} />
|
||||
<button onClick={this.changeCar}>换车</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
class B extends React.Component{
|
||||
// UNSAFE_componentWillReceiveProps
|
||||
componentWillReceiveProps(props){
|
||||
console.log("componentWillReceiveProps")
|
||||
console.log("------")
|
||||
console.log(props)
|
||||
}
|
||||
render(){
|
||||
return (
|
||||
<div>
|
||||
<li>B</li>
|
||||
<li>接收到的值:{this.props.carName}</li>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
ReactDOM.render(
|
||||
// <Count/>,document.getElementById('app')
|
||||
<A/>,document.getElementById('app')
|
||||
)
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,78 @@
|
||||
<!--
|
||||
* @Author: chenxuan
|
||||
* @Date: 2022-01-13 00:00:14
|
||||
* @LastEditTime: 2022-01-13 00:42:36
|
||||
* @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>生命周期函数</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})
|
||||
}
|
||||
death=()=>{
|
||||
ReactDOM.unmountComponentAtNode(document.getElementById('app'))
|
||||
}
|
||||
// 强制更新不受shouldComponentUpdate控制
|
||||
force=()=>{
|
||||
this.forceUpdate()
|
||||
}
|
||||
// 定义在类自身,接收props state两个参数,必须返回状态对象
|
||||
// 返回状态对象后会劫持原有props
|
||||
static getDerivedStateFromProps(props,state){
|
||||
console.log(props,state)
|
||||
return props
|
||||
}
|
||||
render(){
|
||||
console.log("3-render")
|
||||
return (
|
||||
<div>
|
||||
<h2>当前求和为:{this.state.count}</h2>
|
||||
<button onClick={this.add}>点我+1</button>
|
||||
<button onClick={this.death}>销毁</button>
|
||||
<button onClick={this.force}>强制更新</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
componentDidMount(){
|
||||
console.log('4-componentDidMount')
|
||||
}
|
||||
componentWillUnmount(){
|
||||
console.log("5-componentWillUnmount")
|
||||
}
|
||||
// 控制组件更新的阀门
|
||||
shouldComponentUpdate(){
|
||||
console.log('shouldComponentUpdate')
|
||||
return true
|
||||
}
|
||||
|
||||
componentDidUpdate(){
|
||||
console.log('componentDidUpdate')
|
||||
}
|
||||
}
|
||||
ReactDOM.render(
|
||||
<Count count={1999} />,document.getElementById('app')
|
||||
)
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
+6
-1
@@ -1,2 +1,7 @@
|
||||
1、生命周期
|
||||
|
||||
新版中部分生命周期函数需要增加前缀UNSAFE_
|
||||
UNSAFE_componentWillMount
|
||||
UNSAFE_componentWillUpdate
|
||||
UNSAFE_componentWillReceiveProps
|
||||
2、新增生命周函数
|
||||
getDerivedStateFromProps
|
||||
Reference in New Issue
Block a user