zoukankan      html  css  js  c++  java
  • [React] Capture values using the lifecycle hook getSnapshotBeforeUpdate in React 16.3

    getSnapshotBeforeUpdate is a lifecycle hook that was introduced with React 16.3. It is invoked right before the most recently rendered output is committed and the value returned by it will be passed as a third parameter to componentDidUpdate. It enables your component to capture current values for example a scroll position before they are potentially changed.

    import React, { Component } from "react";
    
    class Chat extends Component {
      wrapperRef = React.createRef();
    
      componentDidMount() {
        this.wrapperRef.current.scrollTop = this.wrapperRef.current.scrollHeight;
      }
    
      getSnapshotBeforeUpdate(prevProps, prevState) {
        const wrapper = this.wrapperRef.current;
        return wrapper.scrollTop + wrapper.offsetHeight >= wrapper.scrollHeight;
      }
    
      componentDidUpdate(prevProps, prevState, snapshot) {
        if (snapshot) {
          this.wrapperRef.current.scrollTop = this.wrapperRef.current.scrollHeight;
        }
      }
    
      render() {
        return (
          <div
            style={{
              height: 200,
              overflowY: "scroll",
              border: "1px solid #ccc"
            }}
            ref={this.wrapperRef}
            children={this.props.children}
          >
            {this.props.children}
          </div>
        );
      }
    }
    
    export default Chat;

  • 相关阅读:
    Centos6.8下设置gitlab服务开机自启动,关闭防火墙开机自启动
    gitlab设置SSH key
    在centos6.8下安装gitlab遇到的坑
    recyclerView中的方法
    ListView中的方法
    tcp断开时分几步
    get,post区别
    cookie是什么,在什么地方会用到
    http和https的区别
    keystore是个嘛东西
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9010473.html
Copyright © 2011-2022 走看看