React V7.0.2组件生命周期

355 阅读3分钟

组件的生命周期

生命周期图谱

"react": "^17.0.2",

react生命周期有哪几个阶段

1、挂载时

当组件实例被创建并插入 DOM 中时,其生命周期调用顺序如下:

2、更新时

当组件的 props 或 state 发生变化时会触发更新。组件更新的生命周期调用顺序如下:

3、卸载时

当组件从 DOM 中移除时会调用如下方法:

DerivedStateFromPropsSnapshotBeforeUpdateshouldComponentUpdate来自道具的派生状态更新前快照应该组件更新
//App.js
export default class App extends Component {
	constructor(props) {
		super(props)
		this.state = {
			name: '这是个初始化的数据',
		}
		console.log('father constructor');
	}

	static getDerivedStateFromProps(nextProps, prevState){
		console.log('father getDerivedStateFromProps',nextProps, prevState);
		return null
	}
	shouldComponentUpdate(nextProps,nextState){
		console.log('father shouldComponentUpdate',nextProps,nextState);
		return true
	}

	getSnapshotBeforeUpdate(prevProps, prevState){
		console.log('father getSnapshotBeforeUpdate',prevProps, prevState);
		return null
	}

	componentDidUpdate(){
		console.log('father componentDidUpdate');
	}
  
	componentDidMount(){
		console.log('father componentDidMount');
	}
  
	componentWillUnmount(){
		console.log('father componentWillUnmount');
	}
  
  handleContext = () => {
		console.log('=====子组件调用父组件方法,改变父组件的state值=====')
		this.setState({
			name:'子组件调用父组件方法'
		})
	}
	render() {
		console.log('father render');
		const { name } = this.state
		return (
			<div className="App">
				<div>state值=====:{name}</div>
				<button onClick={()=>{
					console.log('=====改变state值=====');
					this.setState({
						name:'这是个改变state值的数据'
					})
				}}>改变state值</button>
        
        <Children name={name} handleContext={this.handleContext}></Children>
			</div>
		)
	}
}

\

\

componentWillMount 

数据渲染之前,不能和getDerivedStateFromProps、getSnapshotBeforeUpdate同时使用

react的错误处理

当渲染过程,生命周期,或子组件的构造函数中抛出错误时,会调用如下方法:

注意点⚠️:

  • getDerivedStateFromProps()会在调用 render 方法之前调用,并且在初始挂载及后续更新时都会被调用。它应返回一个对象来更新 state,如果返回 null 则不更新任何内容。
  • 此方法适用于,即state的值在任何时候都取决于props,派生状态会导致代码冗余,并使组件难以维护。
  • 谨慎使用。

父子组件的生命周期

\

1、挂载时

  • father constructor
  • father getDerivedStateFromProps
  • father render
  • children constructor
  • children getDerivedStateFromProps
  • children render
  • children componentDidMount
  • father componentDidMount

2、更新时

父组件更新state值时:

=====父组件改变state值=====

  • father getDerivedStateFromProps
  • father shouldComponentUpdate
  • father render
  • children getDerivedStateFromProps
  • children shouldComponentUpdate
  • children render
  • children getSnapshotBeforeUpdate
  • father getSnapshotBeforeUpdate
  • children componentDidUpdate
  • father componentDidUpdate

子组件更新state值时:

=====子组件改变state值=====

  • children getDerivedStateFromProps
  • children shouldComponentUpdate
  • children render
  • children getSnapshotBeforeUpdate
  • children componentDidUpdate

\

=====子组件调用父组件方法,改变父组件的state值=====

同:父组件更新state值时

3、卸载时

\

\

//Children.js
import React, { useEffect,Component } from 'react'
export default class Children extends Component {
    constructor(props){
        super(props)
        this.state={
            count:0
        }
        console.log('children constructor');
    }


    static getDerivedStateFromProps(nextProps, prevState){
      console.log('children getDerivedStateFromProps',nextProps, prevState);
      return null
    }
    shouldComponentUpdate(nextProps,nextState){
      console.log('children shouldComponentUpdate',nextProps,nextState);
      return true
    }

    getSnapshotBeforeUpdate(prevProps, prevState){
      console.log('children getSnapshotBeforeUpdate',prevProps, prevState);
      return null
    }

    componentDidUpdate(){
      console.log('children componentDidUpdate');
    }

    componentDidMount(){
      console.log('children componentDidMount');
    }
    componentWillUnmount(){
      console.log('children componentWillUnmount');
    }

    handleLog = () => {
        console.log("children Component");
    };
    render(){
        const {name,handleContext} = this.props
        const {count} = this.state
        console.log('children render');
        return (
            <div>
                <h1>这是childern页面</h1>
                <div>这是个从父组件通过props值获取的数据:{name}</div>
                <div>这是子组件的state值:{count}</div>
                <button onClick={()=>{
                    console.log('=====子组件改变state值=====');
                    this.setState({
                        count:++this.state.count
                    })
                }}>数字增加</button>
                <button onClick={handleContext}>子组件调用父组件方法</button>
            </div>
        )
    }
}