美文网首页
react生命周期函数

react生命周期函数

作者: 未来在奋斗 | 来源:发表于2019-12-18 11:11 被阅读0次

// 生命周期的钩子函数

三大阶段

  1. 挂载阶段
    // constructor 构造函数
    // 1. 一次生命周期中,只会触发一次
    // 2. 一般是用来做 state 的 初始化工作
    // 3. 使用它来做提前的 事件处理函数 this 绑定问题
    // render 函数
    // 1. 一次生命周期中,触发多次
    // 2. 主要是用来返回 组件的模板内容的
    // 3. 只要 state 、 props 发生变化,那么这个函数默认情况下一定会重新执行。(要看是否处理 shouldComponentUpdate)
    // 4. 如果调用了 force­Update() 的话,那么render一定会重新执行
    // 5. 能否发生ajax请求?(不能)
    // componentWillMount 挂载之前(过期)
    // static getDerivedStateFromProps(props, state) 派生state数据 (react 中的计算属性)
    // 1. 触发在 render 之前,挂载与更新阶段都会触发
    // 2. 必须返回对象或者 null。返回的如果是对象的话,会与 state 对象合并成一份新的 state。如果是 null 的话就不做处理
    // componentDidMount 挂载完成
    // 1. 发生ajax请求
    // 2. 可以得到真实的DOM对象了
  1. 更新阶段
    // static getDerivedStateFromProps(props, state)
    // shouldComponentUpdate(nextProps, nextState) 我是否应该更新呢?
    // 1. 比较重要,是实现性能优化的一个点
    // 2. 必须提供返回值,返回一个布尔类型的值,如果为 true ,更新流程往下走。 如果为 false, 更新流程结束。
    // render 函数
    // componentWillUpdate() 更新之前 (过期了)
    // getSnapshotBeforeUpdate(prevProps, prevState) 替代更新之前
    1. 更新之前,
    2. 必须有返回值或者返回null。返回值的内容会给到
      componentDidUpdate 的第三个参数
      componentDidUpdate(prevProps, prevState, snapshot) 更新完成,并且真实DOM也更新完成
  1. 销毁阶段
    componentWillUnmount 销毁之前
    1. 做一些清理工作。
import React from "react";
import ReactDOM from "react-dom";

// class Hello extends React.Component {
//   constructor() {
//     super();

//     console.log("构造函数");

//     this.state = {
//       msg: "我的天"
//     };
//   }

//   render() {
//     console.log(this.state);

//     return (
//       <div>
//         <h1>Hello, {this.state.name}</h1>
//         <button
//           onClick={() => {
//             this.setState({
//               msg: "天的我"
//             });
//           }}
//         >
//           点我,修改msg
//         </button>
//       </div>
//     );
//   }

//   static getDerivedStateFromProps(props, state) {
//     console.log(props);
//     console.log(state);
//     return {
//       name: "张三"
//     };
//   }

//   componentDidMount() {
//     console.log("挂载完成");
//   }
// }

class Hello extends React.Component {
  state = {
    fruits: ["Apple", "Banana", "Orange"],
    msg: "hello"
  };

  static getDerivedStateFromProps(props, state) {
    console.log("派生");
    return {
      fruitsNum: state.fruits.length
    };
  }

  shouldComponentUpdate(nextProps, nextState) {
    // 如何判断是 fruits 更新了呢?
    if (nextState.fruits.length !== this.state.fruits.length) {
      return true;
    } else {
      return false;
    }
  }

  getSnapshotBeforeUpdate() {
    return "123";
  }

  componentDidUpdate(prevProps, prevState, snapshot) {
    console.log(snapshot);
  }

  render() {
    console.log("render");
    return (
      <div>
        <p>水果数量有:{this.state.fruitsNum}</p>
        <ul>
          {this.state.fruits.map((item, index) => {
            return <li key={index}>{item}</li>;
          })}
        </ul>
        <button
          onClick={() => {
            this.setState({
              fruits: [...this.state.fruits, "西瓜"]
            });
          }}
        >
          点我,新增一个西瓜
        </button>

        <button
          onClick={() => {
            this.setState({
              msg: "world"
            });
          }}
        >
          修改 msg
        </button>
      </div>
    );
  }
}

ReactDOM.render(<Hello />, document.getElementById("root"));

相关文章

  • RN-生命周期函数(新)

    旧版生命周期函数 react 16.4 以后生命周期函数 http://projects.wojtekmaj.pl...

  • React 生命周期函数

    https://reactjs.org/docs/react-component.html React生命周期函数...

  • React的生命周期函数

    一、生命周期函数的定义 在某个时刻自动被调用的函数是生命周期函数 二、React中生命周期函数示意图 三、生命周期...

  • 04.React高级部分(中)

    React的生命周期函数 React生命周期函数的使用场景 这一小节,我们来做两个生命周期相关的常用操作1.如何避...

  • useState

    react-hooks 如果你熟悉 React class 的生命周期函数,你可以把 useEffect Hook...

  • 四:React 进阶三 (生命周期)

    react(一):组件的生命周期 生命周期函数 在组件运行的某个时刻,会被自动执行的一些函数。 React生命周期...

  • React快速上手5-react中的事件和生命周期函数

    这节中我们将主要介绍react应用中的事件和生命周期函数 React事件 在react中,我们不用addEvent...

  • react生命周期函数

    react 生命周期函数介绍[https://www.cnblogs.com/kdcg/p/9182393.htm...

  • 2020-09-11

    REACT生命周期 (JS胖老师课程 ) 生命周期函数指在某一个时刻组件会自动调用执行的函数 REACT生命周期的...

  • React 生命周期

    React 16.4 的生命周期图 早期React生命周期图 从图中,我们看到了一些变化 废弃的三个生命周期函数 ...

网友评论

      本文标题:react生命周期函数

      本文链接:https://www.haomeiwen.com/subject/qjlmnctx.html