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 the render() 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的更多相关文章

  1. React的setState分析

    前端框架层出不穷,不过万变不离其宗,就是从MVC过渡到MVVM.从数据映射到DOM,angular中用的是watcher对象,vue是观察者模式,react就是state了. React通过管理状态实 ...

  2. React中setState同步更新策略

    setState 同步更新 我们在上文中提及,为了提高性能React将setState设置为批次更新,即是异步操作函数,并不能以顺序控制流的方式设置某些事件,我们也不能依赖于this.state来计算 ...

  3. 初学React,setState后获取到的thisstate没变,还是初始state?

    问题:(javascript)初学React,setState后获取到的thisstate没变,还是初始state?描述: getInitialState(){ return {data:[]}; } ...

  4. React中setState学习总结

    react中setState方法到底是异步还是同步,其实这个是分在什么条件下是异步或者同步. 1.先来回顾一下react组件中改变state的几种方式: import React, { Compone ...

  5. React的setState学习及应用

    React的setState学习及应用 一:作用: setState() 将对组件 state 的更改排入队列,并通知 React 需要使用更新后的 state 重新渲染此组件及其子组件.这是用于更新 ...

  6. React组件setState

    注意: 1. 自定义组件首字母必须大写.这里是以函数表达式的方式定义子组件的. 2. 使用 ES6 的 class 关键字创建的 React 组件,组件中的方法遵循与常规 ES6 class 相同的语 ...

  7. React中setState 什么时候是同步的,什么时候是异步的?

    class Example extends React.Component { constructor() { super(); this.state = { val: 0 }; } componen ...

  8. React之setState()

    我们知道,在react中更新单个组件下state中的数据可以用setState()函数来实现,并且可以通过两种传参方式:对象.函数. 另外从文档中我们也可以了解到react可以将多个setState( ...

  9. React的setState执行机制

    1. setState基本特点 1. setState是同步执行的 setState是同步执行的,但是state并不一定会同步更新 2. setState在React生命周期和合成事件中批量覆盖执行 ...

随机推荐

  1. javascript操作window对象

    document.defaultView或全局变量window--获取一个window对象. 1)获取窗体信息 innerHeight.innerWidth--获取窗体内容区域的高度.宽度. oute ...

  2. Spring MVC学习------------核心类与接口

    核心类与接口: 先来了解一下,几个重要的接口与类. 如今不知道他们是干什么的没关系,先混个脸熟,为以后认识他们打个基础. DispatcherServlet   -- 前置控制器 HandlerMap ...

  3. HDU 5493 Queue 树状数组+二分

    Queue Problem Description N people numbered from 1 to N are waiting in a bank for service. They all ...

  4. bzoj5216: [Lydsy2017省队十连测]公路建设

    题目思路挺巧妙的. 感觉应该可以数据结构一波,发现n很小可以搞搞事啊.然后又发现给了512mb,顿时萌生大力线段树记录的念头 一开始想的是记录节点的fa,然后发现搞不动啊?? 但其实边肯定最多只有n- ...

  5. System.DirectoryServices Namespace

    https://docs.microsoft.com/en-us/dotnet/api/system.directoryservices?view=netframework-4.7 The Syste ...

  6. qq邮箱的SMTP服务器是什么

    qq邮箱的SMTP服务器是什么 QQ邮箱POP3 和 SMTP 服务器地址设置如下:POP3服务器地址为“pop.qq.com”,SMTP服务器地址为“smtp.qq.com”注:1.SMTP服务器需 ...

  7. java 中的静态(static)代码块

    类字面常量 final 静态域不会触发类的初始化操作 非 final static 静态域(以及构造器其实是一种隐式的静态方法) Class.forName():会自动的初始化: 使用 .class来 ...

  8. Linux操作系统下Oracle主要监控工具介绍

    Oracle监控包括有效且完全地监控Oracle数据库的性能.可用性和使用率等统计量,还包括即时的错误通知和纠正措施,并提供全面的报表和图表.本文中主要介绍几种Linux操作系统下Oracle主要监控 ...

  9. [jzoj 5664] [GDOI2018Day1模拟4.6] 凫趋雀跃 解题报告(容斥原理)

    interlinkage: https://jzoj.net/senior/#contest/show/2703/3 description: solution: 考虑容斥原理,枚举不合法的走的步数 ...

  10. bfs初学

    BFS: ** 当知道初始和目标状态的,用双向BFS: 无权图最好用BFS 不用重复如队** 实现框架: 抄来的(来源:https://www.luogu.org/blog/stephen2333/s ...