react 生命周期节点

Mounting:挂载阶段
Updating: 运行阶段
Unmounting:卸载阶段
Error Handling:错误处理

import React from 'react';
import ReactDOM from 'react-dom';

class App extends React.Component {
    //构造函数
    constructor(props) {
        super(props)
        this.state = {
            data:'old State'
        }
        console.log('初始化数据','constructor')
    }
    //组件将要加载
    componentWillMount(){
        console.log('componentWillMount')
    }
    //组件加载完成
    componentDidMount(){
        console.log('componentDidMount')
    }
    //将要接收父组件传来的props
    componentWillReceiveProps(){
         console.log('componentWillReceiveProps')
    }
    //子组件 是不是应该更新
    shouldComponentUpdate(){
         console.log('shouldComponentUpdate')
         return true
    }
    // 组件将要更新 shouldComponentUpdate return 为true执行
    componentWillUpdate(){
        console.log('componentWillUpdate')
    }
    // 组件更新完成
    componentDidUpdate(){
        console.log('componentDidUpdate')
    }
    //点击事件
    handleClick(){
        console.log('更新数据')
        this.setState({
            data:'New State'
        })
    }
    //渲染
    render() {
        console.log('render');
        return (
            <div>
                <div>App</div>
            <button onClick={()=>{this.handleClick()}}>更新组件</button>
            </div>
        )
    }
}

ReactDOM.render(
    <App/>,
    document.getElementById('app')
)


//--------------//---------------
打印结果
初始化数据 constructor
componentWillMount
render
componentDidMount

更新数据
shouldComponentUpdate
componentWillUpdate
render
componentDidUpdate

父组件传值

import React from 'react';
import ReactDOM from 'react-dom';
//子组件
class Component extends React.Component {
    //构造函数
    constructor(props) {
        super(props)
        this.state = {
            data:'old State'
        }
        console.log('初始化数据','constructor')
    }
    //组件将要加载
    componentWillMount(){
        console.log('componentWillMount')
    }
    //组件加载完成
    componentDidMount(){
        console.log('componentDidMount')
    }
    //将要接收父组件传来的props
    componentWillReceiveProps(){
         console.log('componentWillReceiveProps')
    }
    //子组件 是不是应该更新
    shouldComponentUpdate(){
         console.log('shouldComponentUpdate')
         return true
    }
    // 组件将要更新 shouldComponentUpdate return 为true执行
    componentWillUpdate(){
        console.log('componentWillUpdate')
    }
    // 组件更新完成
    componentDidUpdate(){
        console.log('componentDidUpdate')
    }
    //组件将要销毁时调用
    componentWillUnmout(){
        console.log('componentWillUnmout')
    }
    //点击事件
    handleClick(){
        console.log('更新数据')
        this.setState({
            data:'New State'
        })
    }
    //渲染
    render() {
        console.log('render');
        return (
            <div>
                <div>Props:{this.props.data}</div>
            <button onClick={()=>{this.handleClick()}}>更新组件</button>
            </div>
        )
    }
}
//父组件
class App extends React.Component{
    //构造函数
    constructor(props) {
        super(props)
        this.state = {
            data:'old Props',
            hasChild:true
        }
        console.log('初始化数据','constructor')
    }
    onPropsChange(){
        console.log('更新props');
        this.setState({
            data:'new Props'
        })
    }
    onDestoryChild(){
        console.log('干点子组件');
        this.setState({
             hasChild:false
        })
    }
    render(){
       return(
           <div>
               {this.state.hasChild?<Component data={this.state.data}/>:null}

               <button onClick={()=>{this.onPropsChange()}}>改变props</button>
               <button onClick={()=>{this.onDestoryChild()}}>干掉子组件</button>
           </div>
       )
    }
}
ReactDOM.render(
    <App/>,
    document.getElementById('app')
)


//--------------//---------------
初始化数据 constructor
componentWillMount
render
componentDidMount

更新props
componentWillReceiveProps
shouldComponentUpdate
componentWillUpdate
render
componentDidUpdate

干点子组件
componentWillUnmout

React生命周期图解是一个描述组件从创建、更新到卸载整个生命周期过程的概念图,它可以帮助开发者理解何时执行哪些操作。主要分为以下几个阶段: 1. **挂载前 (Mounting)**: - `constructor()`: 初始化函数,可以设置状态(state)和绑定事件处理函数。 - `static getDerivedStateFromProps(props, state)`: 首次接收 props 更新时运行,用于基于新的props计算新状态。 - `shouldComponentUpdate(nextProps, nextState)`: 在渲染之前检查是否需要更新,默认会一直更新,除非返回 false。 2. **挂载 (Mounting) 和更新 (Updating)**: - `render()`: 组件的核心部分,返回虚拟DOM。 - `getSnapshotBeforeUpdate(prevProps, prevState)`: 渲染前获取节点快照(如滚动位置),通常不做实际渲染。 - `componentDidMount()`: 成功渲染后立即执行,常用于初始化数据请求或DOM交互。 3. **更新 (Updating)**: - `getDerivedStateFromProps()`: 每次接收到新的props时都会运行,用于修改状态。 - `shouldComponentUpdate()`: 决定是否再次调用 render(),如果返回 true,则继续。 - `render()`: 根据最新的状态和 props 生成新的虚拟 DOM。 - `getSnapshotBeforeUpdate()`: 同上,更新前获取快照。 - `componentDidUpdate(prevProps, prevState, snapshot)`: 更新完成后执行,可用于更新DOM或发送网络请求。 4. **卸载 (Unmounting)**: - `componentWillUnmount()`: 组件即将卸载前执行,常用于清理资源(如清除定时器或取消订阅)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值