React 生命周期

简介: React 生命周期

(九)React生命周期

26.png


16开始生命周期.png

25.png

16之前生命周期.jpg


挂载时= 构造函数创建+渲染+渲染后

挂载阶段

1.constructor生命周期方法中做什么?

- 通过 props接收父组件传递过来的数据

- 通过 this.state 初始化内部的数据

- 通过bind为事件绑定实例(this)

2.render方法

3.componentWillReceiveProps()方法

更新时阶段

1.如果组件更新了则重新渲染,再执行componentDidUpdate方法;

卸载阶段

执行componentWillUnmounted()方法

import React from 'react';
class Home extends React.Component{
    constructor(props){
        super(props);
        console.log('挂载时- 创建组件constructor');
        /*
        1.constructor生命周期方法中做什么?
        - 通过 props接收父组件传递过来的数据
        - 通过 this.state 初始化内部的数据
        - 通过bind为事件绑定实例(this)
        this.myClick = this.btnClick.bind(this);
        * */
        this.state = {
            count: 0
        }
    }
    // 注意点: getDerivedStateFromProps只需要了解即可(用得非常非常的少)
    static getDerivedStateFromProps(props, state){
        console.log('挂载或更新时-映射数据getDerivedStateFromProps');
        console.log(props, state);
        // return {gender: 'man'};
        return props;
    }
    shouldComponentUpdate(nextProps, nextState, nextContext) {
        console.log('更新时-决定是否要更新UIshouldComponentUpdate');
        return true;
        // return false;
    }
    render(){
        if(this.state.count===0){
            console.log('挂载时- 渲染组件render');
        }else{
            console.log('更新时- 渲染组件render');
        }
        /*
        2.render生命周期方法中做什么?
        - 返回组件的网页结构
        * */
        return (
            <div>
                <p>Home</p>
                <p>{this.state.count}</p>
                <button onClick={()=>{this.btnClick()}}>按钮</button>
            </div>
        )
    }
    btnClick(){
        this.setState({
            count: 1
        });
    }
    componentDidMount() {
        console.log('挂载时- 渲染完成componentDidMount');
        /*
         3.componentDidMount生命周期方法中做什么?
         - 依赖于DOM的操作可以在这里进行
         - 在此处发送网络请求就最好的地方(官方建议)
         - 可以在此处添加一些订阅(会在componentWillUnmount取消订阅)
        * */
    }
    componentDidUpdate(prevProps, prevState, snapshot) {
        console.log('更新时- 更新完成componentDidUpdate');
        /*
        4.componentDidUpdate生命周期方法中做什么?
        - 可以在此对更新之后的组件进行操作
        * */
    }
    componentWillUnmount() {
        console.log('卸载时- 即将被卸载componentWillUnmount');
        /*
        5.componentWillUnmount生命周期方法中做什么?
        - 在此方法中执行必要的清理操作
        - 例如,清除 timer,取消网络请求或清除
        - 在 componentDidMount() 中创建的订阅等
        * */
    }
}
class App extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            flag : true
        }
    }
    render(){
        return (
            <div>
                {this.state.flag && <Home/>}
                <button onClick={()=>{this.btnClick()}}>父按钮</button>
            </div>
        )
    }
    btnClick(){
        this.setState({
            flag:false
        })
    }
}
export default App;

24.png

image.png


目录
相关文章
|
19天前
|
前端开发
react生命周期的一些坑
react生命周期的一些坑
|
19天前
|
前端开发 JavaScript 开发者
【第27期】一文了解React生命周期
【第27期】一文了解React生命周期
38 0
|
19天前
|
前端开发
React中生命周期的讲解
React中生命周期的讲解
|
5天前
|
前端开发 JavaScript 开发者
在React中,如何利用生命周期方法管理组件的状态和行为?
【5月更文挑战第29天】在React中,如何利用生命周期方法管理组件的状态和行为?
18 3
|
5天前
|
前端开发 JavaScript 调度
React的生命周期是什么
【5月更文挑战第29天】React的生命周期是什么
13 1
|
19天前
|
前端开发 JavaScript
React生命周期方法在实际开发中的应用场景有哪些?
【4月更文挑战第6天】 React 生命周期方法应用于数据获取、订阅管理、渲染逻辑处理、用户交互响应、性能优化、资源清理、强制更新、错误处理、动画实现、代码分割、服务端渲染、路由处理、依赖注入和集成第三方库。它们帮助控制组件行为和性能,但现代开发推荐使用Hooks替代部分生命周期方法。
19 0
|
19天前
|
前端开发 JavaScript
react生命周期函数
react生命周期函数
11 0
|
19天前
|
前端开发 JavaScript 开发者
浅谈React生命周期
浅谈React生命周期
14 0
|
19天前
|
前端开发
React旧有生命周期和新生命周期的解析
React旧有生命周期和新生命周期的解析
31 0
React旧有生命周期和新生命周期的解析
|
19天前
|
前端开发 JavaScript API
React组件生命周期
React组件生命周期
81 1