详解REACT生命周期 react

shuke 2021-10-3 904

import React, {Component} from 'react';
import {Weather} from "../../api/weather"
class Home extends Component {
  state = {
    d: new Date().toLocaleString()
  }
  //在渲染前调用,在客户端也在服务端。
  componentWillMount() {
    setInterval(() => {
      this.setState({
        d: new Date().toLocaleString()
      })
    }, 2000)
    console.log(1, "componentWillMount")
  }
  //在组件从 DOM 中移除之前立刻被调用。第一次加载不会使用,组件重载时会使用
  componentWillUnmount() {
    // alert("组件将要卸载")
    console.log(2, "componentWillUnmount")
  }
  //在第一次渲染后调用,只在客户端。之后组件已经生成了对应的DOM结构,可以通过this.getDOMNode()来进行访问。 如果你想和其他JavaScript框架一起使用,可以在这个方法中调用setTimeout, setInterval或者发送AJAX请求等操作(防止异步操作阻塞UI)。
  componentDidMount() {
    console.log(3, "componentDidMount")
  }
  componentDidCatch(error, errorInfo) {
    console.log(4, "componentDidCatch")
  }
  //返回一个布尔值。在组件接收到新的props或者state时被调用。在初始化时或者使用forceUpdate时不被调用。
  // 可以在你确认不需要更新组件时使用。
  shouldComponentUpdate(nextProps, nextState, nextContext) {
    console.log(5, "shouldComponentUpdate")
    return true
  }
  //在组件接收到新的props或者state但还没有render时被调用。在初始化时不会被调用。
  componentWillUpdate(nextProps, nextState, nextContext) {
    console.log(6, "componentWillUpdate")
  }
  //在组件完成更新后立即调用。在初始化时不会被调用
  componentDidUpdate(prevProps, prevState, snapshot) {
    console.log(7, "componentDidUpdate")
  }
  //在组件接收到一个新的 prop (更新后)时被调用。这个方法在初始化render时不会被调用。
  componentWillReceiveProps(nextProps, nextContext) {
    console.log(8, "componentWillReceiveProps")
  }
  render() {
    return (
      <dl className="card">
        <dt className="card-header">后台管理首页 <span className="float-right">{this.state.d}</span></dt>
        <dd className="card-body">
        </dd>
      </dl>
    );
  }
}
export default Home;


最新回复 (1)
全部楼主
  • shuke 2021-11-24
    2


    import React, { Component } from 'react'

    export default class Smzq extends Component {
        // 第一阶段:props,state 初始化
        constructor(props) {
            super(props)
            console.log(10"constructor"props.data)
            this.state = {
                title: "生命周期",
                n: 0,
            }
        }
        addn = () => {
            this.setState({ n: this.state.n + 1 })
        }
        componentWillMount() {
            console.log(20"componentWillMount")
        }

        render() {
            // 第阶段 界面渲染
            console.log(30"render"this.state.n)
            return (
                <div className="hand h1">
                    <button onClick={this.addn}>  {this.state.title} {this.state.n}</button>
                   
                </div>
            )
        }
        // 组件挂载完成 ajax,定时器,延时器,事件,DOM操作
        componentDidMount(d) {
            console.log(40"componentDidMount")
        }
        // 组件更新判断   可以在你确认不需要更新组件时使用。
        shouldComponentUpdate(abc) {
            console.log(50"shouldComponentUpdate"abc)
            return true
        }
        //组件更新前
        componentWillUpdate(propslocalstated) {
            console.log(60"componentWillUpdate"props.datalocalstated)
        }

        //组件更新后  
        componentDidUpdate(d) {
            console.log(70"componentDidUpdate")
        }
        //  ("组件将要卸载") 清除定时器,延时器,DOM操作
        componentWillUnmount() {
            
            console.log(80"componentWillUnmount")
        }
        componentDidCatch(a,b,c){
            console.log(100,a,b,c)
        }
        

    }


返回