• React 学习(一) 生命周期


    Lifecycles[^lifecycles]

    图例

    • getDerivedStateFromProps: Keeping the props always is the same as New props

    • shouldComponentUpdate: We can control the components which should be re rendered

    • getSnapshotBeforeUpdate: The snapshot before update

    // Life cycle is for class components, and function components are simulated by hooks
    import React, { Component } from "react";
    
    class Cpn extends Component {
      render() {
        return (
          <div>
            <h2>我是Cpn组件</h2>
          </div>
        );
      }
    
      componentWillUnmount() {
        console.log("componentWillUnmount called");
      }
    }
    
    export default class App extends Component {
      /**
       * 1.init state
       * 2.bind this pointer
       */
      constructor() {
        super();
    
        this.state = {
          count: 0,
          isShow: false,
        };
    
        console.log("constructor called");
      }
    
      render() {
        console.log("render called");
        return (
          <div>
            <span>App</span>
            <h2>Counter: {this.state.count}</h2>
            <button onClick={(e) => this.increment()}>+1</button>
    
            <hr />
            <button onClick={(e) => this.changeCpnShow()}>change</button>
            {this.state.isShow && <Cpn />}
          </div>
        );
      }
    
      changeCpnShow() {
        this.setState({
          isShow: !this.state.isShow,
        });
      }
    
      increment() {
        this.setState({
          count: this.state.count + 1,
        });
      }
    
      /**
       * 1.Operation dependent on DOM
       * 2.Send the network(Vue->created)
       * 3.add some subscribe(unsubscribe in componentWillUnmount)
       */
      componentDidMount() {
        console.log("componentDidMount called");
      }
    
      /**
       * 1.Be called after component updated
       * 2.Compare the props
       */
      componentDidUpdate(prevProps, prevState, snapshot) {
        console.log("componentDidUpdate called");
      }
    }
    

    shouldComponentUpdate[^shouldComponentUpdate]

    // Use the shouldComponentUpdate controls the render function
    import React, { Component } from "react";
    
    export default class App extends Component {
      constructor() {
        super();
    
        this.state = {
          counter: 0,
          message: "Hello World",
        };
      }
    
      render() {
        console.log("App called");
        return (
          <div>
            <h2>counter: {this.state.counter}</h2>
            <h2>message: {this.state.message}</h2>
            <button
              onClick={(e) => {
                this.increment();
              }}
            >
              +
            </button>
            <button
              onClick={(e) => {
                this.changeText();
              }}
            >
              ChangeText
            </button>
          </div>
        );
      }
    
      // Determine whether the component is updated
      shouldComponentUpdate(nextProps, nextState) {
        // console.log("nextState", nextState);
        if (this.state.counter !== nextState.counter) return true;
        return false;
      }
    
      increment() {
        this.setState({
          counter: this.state.counter + 1,
        });
      }
    
      changeText() {
        this.setState({
          message: "Small Stars",
        });
      }
    }
    
    
    每天进步一点点
  • 相关阅读:
    Android面试题目整理与解说(二)
    大学?做码农?做project师?
    图形学领域的关键算法及源代码链接
    假设在本地搭一个server和mysql数据库环境,假设使用java来訪问数据库
    [容斥原理] hdu 4135 Co-prime
    leetcode第一刷_Merge Intervals
    关于HashMap的一些深入探索与理解
    摄像头拍照上传
    rowid快速分页解析
    flare-spork: 自己维护的Pig on Spark项目
  • 原文地址:https://www.cnblogs.com/smallstars/p/14043327.html
Copyright © 2020-2023  润新知