no message

This commit is contained in:
chenxuan
2024-04-18 18:07:24 +08:00
parent 204055c000
commit ace175b3d1
+29 -4
View File
@@ -1,18 +1,43 @@
import React from 'react';
class Life extends React.Component {
import ReactDOM from 'react-dom';
class Child extends React.Component{
state={count:0}
componentDidMount() {
console.log('componentDidMount');
}
componentDidUpdate() {
console.log('componentDidUpdate');
}
componentWillUnmount() {
console.log('componentWillUnmount');
// 劫持state
// state属性与props相关时
static getDerivedStateFromProps(props,state){
console.log("getDerivedStateFromProps")
// return {count:"xxx"}
return null;
}
add=()=>{
let {count}=this.state;
count+=1;
this.setState({count})
}
render(){
return (
<div>
<h1>子组件</h1>
<h3>state:{this.state.count}</h3>
<h3>props:{this.props.name}</h3>
<button onClick={this.add}>子组件点击事件</button>
</div>
)
}
}
class Life extends React.Component {
render() {
return (
<div>
<h1>生命周期函数</h1>
<Child name="chenxuan" />
</div>
);
}