[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 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"));

原文地址:https://www.cnblogs.com/Answer1215/p/8511330.html

时间: 2024-10-03 21:34:22

[React] Understanding setState in componentDidMount to Measure Elements Without Transient UI State的相关文章

React中setState同步更新策略

本文和大家分享的主要是React中setState同步更新相关内容,希望对大家学习React有所帮助. 为了提高性能React将setState设置为批次更新,即是异步操作函数,并不能以顺序控制流的方式设置某些事件,我们也不能依赖于 this.state 来计算未来状态.典型的譬如我们希望在从服务端抓取数据并且渲染到界面之后,再隐藏加载进度条或者外部加载提示: componentDidMount() { fetch('https://example.com') .then((res) => re

React的setState分析

前端框架层出不穷,不过万变不离其宗,就是从MVC过渡到MVVM.从数据映射到DOM,angular中用的是watcher对象,vue是观察者模式,react就是state了. React通过管理状态实现对组件的管理,通过this.state()方法更新state.当this.setState()被调用的时候,React会重新调用render方法来重新渲染UI. 本文针对React的SetState的源码来进行解读,根据陈屹老师的<深入React技术栈>加上自己的理解. 1. setState异

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

class Example extends React.Component { constructor() { super(); this.state = { val: 0 }; } componentDidMount() { this.setState({val: this.state.val + 1}); console.log(this.state.val); // 第 1 次 log this.setState({val: this.state.val + 1}); console.lo

React的setState执行机制

1. setState基本特点 1. setState是同步执行的 setState是同步执行的,但是state并不一定会同步更新 2. setState在React生命周期和合成事件中批量覆盖执行 在React的生命周期钩子和合成事件中,多次执行setState,会批量执行 具体表现为,多次同步执行的setState只有最后一次起作用,前面的全被覆盖 当遇到多个setState调用时候,会提取单次传递setState的对象,把他们合并在一起形成一个新的单一对象,并用这个单一的对象去做setSt

【react】ReactJS中几个比较最优秀的UI框架

一.Material-UI Material-UI是一款React组件库来实现Google的Material Design风格UI界面框架.也是首个React的UI工具集之一.使用它可以快速搭建出赏心悦目的应用界面. 英文文档:https://material-ui.com 中文文档:http://design.1sters.com Github: https://github.com/mui-org/material-ui 二.React-Bootstrap React-Bootstrap是可

React(0.13) 利用componentDidMount 方法设置一个定时器

<html> <head> <title>hello world React.js</title> <script src="build_0.13/react.min.js"></script> <script src="build_0.13/JSXTransformer.js"></script> </head> <body> <div i

[React] Call setState with null to Avoid Triggering an Update in React 16

Sometimes it’s desired to decide within an updater function if an update to re-render should be triggered. Calling .setState with null no longer triggers an update in React 16. This means we can decided if the state gets updated within our .setState 

React 的setState 理解

我们都知道在React中,setState() 方法是用来改变组件状态的,在项目中也一直用,也没有出现什么问题(使用方法太简单了),但今天看了一篇文章,提到了setState 使用时的两个注意点,加深了对setState()的认识. setState() 最简单的使用方式,就是给它传递一个对象,对象中的属性就是我们要改变的状态,对象中只写我们要改变的那些状态就可以了,react 会把我们这次所做的改变和原来没有做改变的状态进行合并,形成最新的状态,重新渲染组件.写一个简单的例子,点击按钮显示点击

【React 6/100】 React原理 | setState | JSX语法转换 | 组件更新机制

****关键字 | setState | JSX语法转换 | 组件更新机制 组件更新机制 setState() 的两个作用 修改state 更新组件 过程:父组件重新渲染时,也会重新渲染子组件,但只会渲染当前组件子树(当前组件以其所有子组件) 组件性能优化 减轻state 减轻state:只存储跟组件渲染相关的数据(比如:count/ 列表数据 /loading等) 注意:不用做渲染的数据不要放在state中 对于这种需要在多个方法中用到的数据,应该放到this中 避免不必要的重新渲染 组件更新