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: ,
overflowY: "scroll",
border: "1px solid #ccc"
}}
ref={this.wrapperRef}
children={this.props.children}
>
{this.props.children}
</div>
);
}
} export default Chat;

[React] Capture values using the lifecycle hook getSnapshotBeforeUpdate in React 16.3的更多相关文章

  1. [React] Update State Based on Props using the Lifecycle Hook getDerivedStateFromProps in React16.3

    getDerivedStateFromProps is lifecycle hook introduced with React 16.3 and intended as a replacement ...

  2. [MST] Loading Data from the Server using lifecycle hook

    Let's stop hardcoding our initial state and fetch it from the server instead. In this lesson you wil ...

  3. React教程:4 个 useState Hook 示例

    摘要: React示例教程. 原文:快速了解 React Hooks 原理 译者:前端小智 到 React 16.8 目前为止,如果编写函数组件,然后遇到需要添加状态的情况,咱们就必须将组件转换为类组 ...

  4. React简单教程-4-事件和hook

    前言 在上一章 React 简单教程-3-样式 中我们建立了一个子组件,并稍微美化了一下.在另一篇文章 React 简单教程-3.1-样式之使用 tailwindcss 章我们使用了 tailwind ...

  5. 关于React.PropTypes的废除,以及新版本下的react的验证方式

    React.PropTypes是React用来typechecking的一个属性.要在组件的props上运行typechecking,可以分配特殊的propTypes属性: class Greetin ...

  6. React实战教程之从零开始手把手教你使用 React 最新特性Hooks API 打造一款计算机知识测验App

    项目演示地址 项目演示地址 项目代码结构 前言 React 框架的优雅不言而喻,组件化的编程思想使得React框架开发的项目代码简洁,易懂,但早期 React 类组件的写法略显繁琐.React Hoo ...

  7. react系列笔记1 用npx npm命令创建react app

    react系列笔记1 用npx npm命令创建react app create-react-app my-app是开始构建新的 React 单页应用程序的最佳方式.它已经为你设置好了开发环境,以便您可 ...

  8. react全家桶从0搭建一个完整的react项目(react-router4、redux、redux-saga)

    react全家桶从0到1(最新) 本文从零开始,逐步讲解如何用react全家桶搭建一个完整的react项目.文中针对react.webpack.babel.react-route.redux.redu ...

  9. [React] Create a Persistent Reference to a Value Using React useRef Hook

    The useRef is a hook for creating values that persist across renders. In this lesson we'll learn how ...

随机推荐

  1. PCB 后台自动系统集成与邮件推送实现

    在PCB行业中,工程系统是主要数据生产者,而这些数据不仅仅给自己系统使用呀,我们需要将数据传递到各系统,才达到各系统共同协作的目的. 这里以问答方式对实现方式进行讲解.呵呵呵! 后台自动集成问题解答: ...

  2. hdu1429胜利大逃亡(bfs)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  3. 6月10遇到的bug

    3.遇到的BUG        1.@Service(version = "1.0.0", interfaceClass = TransactionItemService.clas ...

  4. SQLServer2008 关于数据转换

    全进位 select cast(ceiling(2.1111) as dec(18,0)) 结果:3

  5. cannot connect to host的解决办法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 下午更新源码,出现下面的错误: 通过ping来测试svn服务器的连接,发现可以连接得通,于是猜测可以服务器的svn服务 ...

  6. 打开手机摄像头扫描二维码或条形码全部操作(代码写的不好,请提出指教,共同进步,我只是一个Android的小白)

    (1)下载二维码的库源码 链接:http://pan.baidu.com/s/1pKQyw2n 密码:r5bv 下载完成后打开可以看到 libzxing 的文件夹,最后添加进 Android  Stu ...

  7. 协议(Protocol) 和代理(Delegate)

    1.概念与组成 delegate是iOS中一种常见的设计模式,是一种消息传递的的方式,常见的消息传递方式还有以下几种: 通知:在iOS中由通知中心进行消息接收和消息广播,是一种一对多的消息传递方式. ...

  8. iconfont在ios(safari)中的坑

    最近公司决定将项目图标整体迁移到iconfont,按网上常规方法,在安卓.pc端都没问题,唯独在ios的safari浏览器及微信内置浏览器中,iconfont始终在正常位置向下偏移,导致图标错乱. 网 ...

  9. 【Oracle】 手工建库

    操作系统:OEL 5.6 数据库版本:Oracle11gR2  11.2.0.4.0 新建数据库名称:lgr 1 生成pfile和口令文件 1)生成pfile文件,在模板文件init.ora中提取 [ ...

  10. Eclipse Rap开发 异步刷新UI处理

    1.Display.getCurrent()获取的是当前线程的display对象,如果当前在非UI线程中那么获取到的display对象为空:      一般Display.getCurrent() 用 ...