[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 ...
随机推荐
- django模型层(二)
多表操作 创建模型 实例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之间是一对 ...
- 最近积累的JS 东西,分享一下
js 关闭页面 var browserName=navigator.appName; if (browserName=="Netscape") { window.open('',' ...
- SQLServer2008 将“单个用户”改为“多用户”
一开始是要想要分离掉数据库,然后将其删除 不知道为什么一直分离不了,试了很多次,又尝试直接删除 结果数据库突然显示成了“单个用户” 尝试查看其属性,或者“新建查询”也都报错,提示已经有其他用户建立了连 ...
- Android 关于Fragment重叠问题分析和解决
一.问题描述 相信大家在使用Fragment的过程中,肯定碰到过Fragment重叠的问题,重启应用就好了.然而原因是什么呢? 二.原因分析 首先,Android管理Fragment有两种方式,使用a ...
- Android 解决小米手机Android Studio安装app 报错的问题It is possible that this issue is resolved by uninstalling an existi
Android Studio升级到2.3版本之后,小米手机MIUI8不能运行Android Studio程序,报如下错误: Installation failed with message Faile ...
- WIN10 64位下VS2015 C#直接添加 halcon 12导出的CS文件实现视觉检测
C# halcon 12 联合编程的 实例 1.先调试好halcon程序,我以读取图片的程序为例.
- OpenCV: 图像连通域检测的递归算法
序言:清除链接边缘,可以使用数组进行递归运算; 连通域检测的递归算法是定义级别的检测算法,且是无优化和无语义失误的. 同样可用于寻找连通域 void ClearEdge(CvMat* MM,CvPoi ...
- python处理中文编码
python2 读取excle中的数据时,对于汉字的读取报错: 代码:data[num][4]={"content": "测试"} data=data[num] ...
- Windows Live Writer 历史Blog修改的功能
其实 WLW 有历史Blog修改的功能,我只是一直没有找到,就在打开“最近发布的日志”里面, 位于屏幕的右侧“打开”列表下. 最近发现记忆力越来越差了,BLOG看来是必须的了.
- js对cookie增删改查的封装
/** * 获取cookie * @param name * @returns {*} */ function getCookie(name) { var cookieArr = document.c ...