[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: ,
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的更多相关文章
- [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 ...
- [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 ...
- React教程:4 个 useState Hook 示例
摘要: React示例教程. 原文:快速了解 React Hooks 原理 译者:前端小智 到 React 16.8 目前为止,如果编写函数组件,然后遇到需要添加状态的情况,咱们就必须将组件转换为类组 ...
- React简单教程-4-事件和hook
前言 在上一章 React 简单教程-3-样式 中我们建立了一个子组件,并稍微美化了一下.在另一篇文章 React 简单教程-3.1-样式之使用 tailwindcss 章我们使用了 tailwind ...
- 关于React.PropTypes的废除,以及新版本下的react的验证方式
React.PropTypes是React用来typechecking的一个属性.要在组件的props上运行typechecking,可以分配特殊的propTypes属性: class Greetin ...
- React实战教程之从零开始手把手教你使用 React 最新特性Hooks API 打造一款计算机知识测验App
项目演示地址 项目演示地址 项目代码结构 前言 React 框架的优雅不言而喻,组件化的编程思想使得React框架开发的项目代码简洁,易懂,但早期 React 类组件的写法略显繁琐.React Hoo ...
- react系列笔记1 用npx npm命令创建react app
react系列笔记1 用npx npm命令创建react app create-react-app my-app是开始构建新的 React 单页应用程序的最佳方式.它已经为你设置好了开发环境,以便您可 ...
- react全家桶从0搭建一个完整的react项目(react-router4、redux、redux-saga)
react全家桶从0到1(最新) 本文从零开始,逐步讲解如何用react全家桶搭建一个完整的react项目.文中针对react.webpack.babel.react-route.redux.redu ...
- [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 ...
随机推荐
- PCB Genesis加邮票孔(弧形连接位宽度校正)实现算法
采用弧形作为加接位,当两边距离较远时,会造成连接位变窄,由于之前算法是基于连接位间距做为半径画弧, 必然存在这个缺陷,这边做少许的改进解决此问题. 现将几个种增加孤形连接位的图形对比如下: 一.两边外 ...
- E20170809-mk
collapse n. 垮台; (身体的) 衰弱; vt. 使倒塌; 使坍塌; 使瓦解; vi. 崩溃; 倒塌; 折叠; (尤指工作劳累后 ...
- windows系统下nodejs安装、环境配置及删除NPM全局配置
nodejs安装及设置NPM全局路径 删除NPM全局路径配置 一.nodejs安装及设置NPM全局路径 第一步:下载安装文件 下载nodejs,官网:http://nodejs.org/downloa ...
- BZOJ 2118 Dijkstra
思路: 经典题 不解释 找到最小的数mn 所有都是在mod mn的意义下 搞得 i->(i+a[i])%mn 边权为a[i] //By SiriusRen #include <queue ...
- 2013 ACM/ICPC Asia Regional Changsha Online - J
原题戳这里. 题意: 有一未知列数a1,a2,a3.....an, 已知s[i]=a[i-1]+a[i]+a[i] (1<i<n) s[1]=a[1]+a[2]; s[n]=a[n-1] ...
- Java中的常用类有哪些
1NumberFormat 2DecimalFormat 3BigDecimal 4Math 5Random 6DateFormat 7SimpleDateFormat 8Calendar 9Date ...
- 9 在C#控制台程序(console)中让用户输入
经过前面那些练习,我们已经熟悉录入一些简单的代码.这些代码可以进行一些简单的运算,在dos窗口打印出一些东西出来.我们现在要开始学习如何把数据从外部输入到我们的程序中. 其实大多数程序的工作是完成下面 ...
- Android嵌入式(初稿)--路漫漫其修远兮,吾将上下而求索
- STL之map篇
度熊所居住的 D 国,是一个完全尊重人权的国度.以至于这个国家的所有人命名自己的名字都非常奇怪.一个人的名字由若干个字符组成,同样的,这些字符的全排列的结果中的每一个字符串,也都是这个人的名字.例如, ...
- 终极解决VS2015 安装失败问题,如 安装包损坏或丢失
1.去微软官网下载完成ISO镜像,最好不要在线安装, 打开官方链接 https://www.visualstudio.com/zh-cn/downloads/download-visual-studi ...