[React] Understanding setState in componentDidMount to Measure Elements Without Transient UI State
In this lesson we'll explore using setState
to synchronously update in componentDidMount
. This allows for us to use getBoundingClientRect
or other synchronous UI calls and make changes to the UI without a transient UI state.
componentDidMount()
is invoked immediately after a component is mounted. Initialization that requires DOM nodes should go here. If you need to load data from a remote endpoint, this is a good place to instantiate the network request.This method is a good place to set up any subscriptions. If you do that, don’t forget to unsubscribe in
componentWillUnmount()
.Calling
setState()
in this method will trigger an extra rendering, but it will happen before the browser updates the screen. This guarantees that even though therender()
will be called twice in this case, the user won’t see the intermediate state. Use this pattern with caution because it often causes performance issues. It can, however, be necessary for cases like modals and tooltips when you need to measure a DOM node before rendering something that depends on its size or position.
That being said, using setState in componentDidMount:
- which is good for fetching data from API.
- which is good for Modal and tooltip component which related to position.
- because render() functions is called twice, be careful about proferemce issue.
import React, { Component } from "react";
import { render } from "react-dom"; class App extends Component {
constructor(props) {
super(props);
this.state = {
width: 0,
height: 0
};
}
componentDidMount() {
const { width, height } = this.r.getBoundingClientRect();
this.setState({
width,
height
});
}
render() {
console.count("render");
return (
<div>
<h2 ref={r => (this.r = r)}>
{this.state.width} x {this.state.height}
</h2>
</div>
);
}
} render(<App />, document.getElementById("root"));
[React] Understanding setState in componentDidMount to Measure Elements Without Transient UI State的更多相关文章
- React的setState分析
前端框架层出不穷,不过万变不离其宗,就是从MVC过渡到MVVM.从数据映射到DOM,angular中用的是watcher对象,vue是观察者模式,react就是state了. React通过管理状态实 ...
- React中setState同步更新策略
setState 同步更新 我们在上文中提及,为了提高性能React将setState设置为批次更新,即是异步操作函数,并不能以顺序控制流的方式设置某些事件,我们也不能依赖于this.state来计算 ...
- 初学React,setState后获取到的thisstate没变,还是初始state?
问题:(javascript)初学React,setState后获取到的thisstate没变,还是初始state?描述: getInitialState(){ return {data:[]}; } ...
- React中setState学习总结
react中setState方法到底是异步还是同步,其实这个是分在什么条件下是异步或者同步. 1.先来回顾一下react组件中改变state的几种方式: import React, { Compone ...
- React的setState学习及应用
React的setState学习及应用 一:作用: setState() 将对组件 state 的更改排入队列,并通知 React 需要使用更新后的 state 重新渲染此组件及其子组件.这是用于更新 ...
- React组件setState
注意: 1. 自定义组件首字母必须大写.这里是以函数表达式的方式定义子组件的. 2. 使用 ES6 的 class 关键字创建的 React 组件,组件中的方法遵循与常规 ES6 class 相同的语 ...
- React中setState 什么时候是同步的,什么时候是异步的?
class Example extends React.Component { constructor() { super(); this.state = { val: 0 }; } componen ...
- React之setState()
我们知道,在react中更新单个组件下state中的数据可以用setState()函数来实现,并且可以通过两种传参方式:对象.函数. 另外从文档中我们也可以了解到react可以将多个setState( ...
- React的setState执行机制
1. setState基本特点 1. setState是同步执行的 setState是同步执行的,但是state并不一定会同步更新 2. setState在React生命周期和合成事件中批量覆盖执行 ...
随机推荐
- test environment bug
pxj::EsgServiceImpl.kmsProcess->Get:ESG JAR return data is:{"code":10000,"data&quo ...
- 使用postman 测试restful接口
前提: 在chrome网上应用商店安装postman 和 Postman Interceptor. 如下图: 使用postman的时候,最后开启postman Interceptor,如下图: 然后启 ...
- Apache负载均衡与Tomcat集群配置学习(Windows环境)
本文主要参考自http://www.iteye.com/topic/985404?dhcc,经由实际操作配置操并记录而成. 由于最近的一个Java开发项目用到了Tomcat中间件作为web服务器,刚开 ...
- HDFS学习笔记(1)初探HDFS
Hadoop分布式文件系统(Hadoop Distributed File System, HDFS) 分布式文件系统是一种同意文件通过网络在多台主机上分享的文件系统.可让多机器上的多用户分享文件和存 ...
- CDH使用秘籍(一):Cloudera Manager和Managed Service的数据库
背景 从业务发展需求,大数据平台须要使用spark作为机器学习.数据挖掘.实时计算等工作,所以决定使用Cloudera Manager5.2.0版本号和CDH5. 曾经搭建过Cloudera Mana ...
- 5.文件I/O
1 C标准函数与系统函数的区别 文件的结构体: 1.1 I/O缓冲区 每一个FILE文件流都有一个缓冲区buffer,默认大小8192Byte. 1.2 效率 文件缓冲区会降低效率.这里提供缓冲区主要 ...
- 使用右键打开Visual Code
Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT\*\shell\Visual Code]@="Edit with Visual ...
- Spring学习笔记(一) 简介
版权声明 本文是摘自IBM上Naveen Balani的一篇文章,原文请点击此处:http://www.ibm.com/developerworks/cn/java/wa-spring1/ Sprin ...
- Bluefish Editor - 蓝鱼编辑器 for Web Designer
Bluefish is a GTK+ HTML editor for the experienced web designer. Its features include nice wizards f ...
- P2740 [USACO4.2]草地排水Drainage Ditches
题目背景 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰修建了一套排水系统来使贝茜的草地免除被大水淹没 ...