React旧版本生命周期

React旧版本生命周期

一. 组件挂载的生命周期执行顺序
     *  由ReactDOM.render()触发 ---第一次渲染
     *  1. constructor 构造器
     *  2. componentWillMount 组件将要挂载
     *  3. render
     *  4. componentDidMount 组件挂载完毕 
   
二.组件正常更新的时候生命周期执行顺序
     a)  由setState() 或 父组件 render() 调用
     *  1. shouldComponentUpdate 控制组件更新的"开关"
     *  2. componentWillUpdate 组件将要更新的钩子
     *  3. render
     *  4. componentDidUpdate 组件更新完毕的钩子
     * b)组件强制更新,对组件的状态不做任何更改,不由setDtate调用,由forceUpdate调用
     *  1. componentWillUpdate
     *  2. render
     *  2. componentDidUpdate

三.组件卸载的时候生命周期执行顺序
     *  1. componentWillUnmount 组件销毁
     *      做一些收尾: 关闭定时器 取消订阅

代码(可复制直接运行)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    
</head>
<body>
    <div id="app"></div>
    <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
    <script src="https://cdn.bootcss.com/babel-standalone/6.22.1/babel.min.js"></script>
    <script type="text/babel">
        class Life extends React.Component {
            constructor (props) {
                super(props)
                this.state = {
                    count: 0
                }
                console.log("Demo--- constructor")
            }
            // 更新组件
            handleClick = ()=> {
                this.setState({
                    count: this.state.count + 1
                })
            }
            // 强制更新组件
            force = ()=> {
                this.forceUpdate()
            }
            // 销毁
            destruction = ()=> {
                ReactDOM.unmountComponentAtNode(document.getElementById("app"))
            }
            // 组件将要挂载的钩子
            UNSAFE_componentWillMount(){
                console.log('Demo组件将要挂载--- componentWillMount')
            }
            // 组件挂载完毕的钩子
            componentDidMount() {
                console.log('Demo组件挂载完毕--- componentDidMount')
            }
            // 控制组件更新的"开关" 必须返回一个Boolean
            shouldComponentUpdate() {
                console.log('Demo控制组件更新的"开关"--- shouldComponentUpdate')
                return true
            }
            // 组件将要更新的钩子
            UNSAFE_componentWillUpdate(){
                console.log('Demo组件将要更新--- componentWillUpdate')
            }
            // 组件更新完毕的钩子
            componentDidUpdate(){
                console.log('Demo组件更新完毕--- componentDidUpdate')
            }
            // 组件销毁的钩子
            componentWillUnmount(){
                console.log('Demo--- componentWillUnmount')
            }
            render() {
                console.log('Demo--- render')
                return (
                    <div>
                        <div>当前和为{this.state.count}</div>
                        <button onClick={this.handleClick}>点我加1</button>&nbsp;
                        <button onClick={this.force}>强制更新</button><br/>
                        <button onClick={this.destruction}>点我销毁</button>
                    </div>
                )
            }
        }
        
        // 父组件
        class Father extends React.Component {
            state = {
                money: 100
            }
            giveMoney = () => {
                this.setState({
                    money: 1000
                })
            }
            // 组件将要挂载的钩子
            UNSAFE_componentWillMount(){
                console.log('父组件将要挂载--- componentWillMount')
            }
            // 组件挂载完毕的钩子
            componentDidMount() {
                console.log('父组件挂载完毕--- componentDidMount')
            }
            // 控制组件更新的"开关" 必须返回一个Boolean
            shouldComponentUpdate() {
                console.log('父组件控制组件更新的"开关"--- shouldComponentUpdate')
                return true
            }
            // 组件将要更新的钩子
            UNSAFE_componentWillUpdate(){
                console.log('父组件将要更新--- componentWillUpdate')
            }
            // 组件更新完毕的钩子
            componentDidUpdate(){
                console.log('父组件更新完毕--- componentDidUpdate')
            }
            render() {
                console.log("父组件----render")
                return (
                    <div>
                        <div>父组件</div>
                        <button onClick={this.giveMoney}>给你1000块</button>
                        <Son money={this.state.money}/> 
                    </div>
                )
            }
        }
        // 子组件
        class Son extends React.Component {
            // 组件将要挂载的钩子
            UNSAFE_componentWillMount(){
                console.log('子组件将要挂载--- componentWillMount')
            }
            // 组件挂载完毕的钩子
            componentDidMount() {
                console.log('子组件挂载完毕--- componentDidMount')
            }
            // 组件将要接受新的props的钩子函数 第一次时不会调用
            UNSAFE_componentWillReceiveProps(props) {
                console.log(props)
                console.log("子组件-----componentWillReceiveProps")
            }
            // 控制组件更新的"开关" 必须返回一个Boolean
            shouldComponentUpdate() {
                console.log('子组件控制组件更新的"开关"--- shouldComponentUpdate')
                return true
            }
            // 组件将要更新的钩子
            UNSAFE_componentWillUpdate(){
                console.log('子组件组件将要更新--- componentWillUpdate')
            }
            // 组件更新完毕的钩子
            componentDidUpdate(){
                console.log('子组件组件更新完毕--- componentDidUpdate')
            }
            render() {
                console.log("子组件----render")
                return (
                    <div>
                        子组件 {this.props.money}
                    </div>
                )
            }
        }
        
        ReactDOM.render(
            <Life/>,
            document.getElementById('app')
        );
    </script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值