# React 生命周期

# 创建阶段

钩子函数

# constructor

构造函数,组件被创建时调用一次,有且只调用一次,为默认state赋初始值。

# componentWillMount

组件即将被渲染的时候调用,render()函数之前调用,有且只调用一次,在组件渲染之前进行的操作:操作默认state

componentWillMount(){
    console.log("组建即将被渲染");
    console.log(this.state);

    //对默认state进行处理
  }
1
2
3
4
5
6

# componentDidMount

组件已经被渲染之后调用,render()函数之后,有且只调用一次

componentDidMount(){
  //render函数执行之后调用
  //发送请求,请求数据
}
1
2
3
4

# render()

组件state发生变化,就会执行。

render(){

}
1
2
3

# 更新阶段

# componentWillReceiveProps

组件将要接受props时调用,多次调用,只要有数据props更新,它就会被调用

componentWillReceiveProps(props){
    console.log("组件即将接受数据");
    console.log(props)

    //更新当前组件的state
    this.setState({
      image:this.props.image
    })
  }
1
2
3
4
5
6
7
8
9

# shouldComponentUpdate

在接受数据函数调用之后调用,用于阻止当前组件更新

shouldComponentUpdate(){
  console.log("组建被更新之前阻止");

  //state验证

  //true 不阻止
  return false;
}
1
2
3
4
5
6
7
8

# componentWillUpdate

如果不阻止更新,在render之前调用

componentWillUpdate(){
    console.log("组件即将被更新");
  }
1
2
3

# componentDidUpdate

更新之后,render渲染结束调用

componentDidUpdate(){
    console.-log("组件被渲染完成之后调用");
  }
1
2
3

# 销毁阶段

componentWillUnmount()

在组件被卸载和销毁之前立刻调用。可以在该方法里处理任何必要的清理工作,例如解绑定时器,取消网络请求,清理任何在componentDidMount环节创建的DOM元素。

Last Updated: 8/9/2022, 4:24:39 PM