一段很普通的代码,出发了两次render

  1. import React, { useState, useEffect } from 'react'
  2. const MouseTracker: React.FC = () => {
  3. const [ positions, setPositions ] = useState({x: 0, y: 0})
  4. useEffect(() => {
  5. console.log('add effect', positions.x)
  6. const updateMouse= (e: MouseEvent) => {
  7. console.log('inner')
  8. setPositions({ x: e.clientX, y: e.clientY })
  9. }
  10. document.addEventListener('click', updateMouse)
  11. return () => {
  12. console.log('remove effect', positions.x)
  13. document.removeEventListener('click', updateMouse)
  14. }
  15. },[])
  16. console.log('before render', positions.x) // 执行了两次
  17. return (
  18. <>
  19. <p>X: {positions.x}, Y : {positions.y}</p>
  20. </>
  21. )
  22. }
  23. export default MouseTracker

原因:

最近的react版本,dev模式下render使用的是strict mode,strict mode的通过两次调用constructor和render函数来更好的检测不符合预期的side effects

文档中有表明

Strict mode can’t automatically detect side effects for you, but it can help you spot them by making them a little more deterministic. This is done by intentionally double-invoking the following functions:

  • Class component constructor, render, and shouldComponentUpdate methods
  • Class component static getDerivedStateFromProps method
  • Function component bodies
  • State updater functions (the first argument to setState)
  • Functions passed to useState, useMemo, or useReducer

下列函数会执行两次

  • 类组件的constructor,render和shouldComponentUpdate方法
  • 类组建的静态方法getDerivedStateFromProps
  • 函数组件方法体
  • 状态更新函数(setState的第一个参数)
  • 传入useState,useMemo或useReducer的函数

在production环境下不会这样,所以不用担心

React 16.13.1触发两次render的更多相关文章

  1. [React] Render Elements Outside the Current React Tree using Portals in React 16

    By default the React Component Tree directly maps to the DOM Tree. In some cases when you have UI el ...

  2. [React] Render Text Only Components in React 16

    In this session we create a comment component to explore how to create components that only render t ...

  3. React 16 加载性能优化指南

    关于 React 应用加载的优化,其实网上类似的文章已经有太多太多了,随便一搜就是一堆,已经成为了一个老生常谈的问题. 但随着 React 16 和 Webpack 4.0 的发布,很多过去的优化手段 ...

  4. 盘点 React 16.0 ~ 16.5 主要更新及其应用

    目录 0. 生命周期函数的更新 1. 全新的 Content API 2. React Strict Mode 3. Portal 4. Refs 5. Fragment 6. 其他 7. 总结 生命 ...

  5. Facebook发布React 16 专利条款改为MIT开源协议

    9 月 26 日,用于构建 UI 的 JavaScript 库 React 16 的最新版本上线. Facebook 最终在现有的两种 React 版本中选择了出现 bug 概率最少的一款.这次版本更 ...

  6. react 16 ssr的重构踩坑

    ssr 服务端不能识别前端的window.特别是首屏渲染的数据需要用到window对象(比如href += location.search); 服务端不能加载图片,css文件. require.ext ...

  7. [译] React 16.3(.0-alpha) 新特性

    原文地址:What's new in React 16.3(.0-alpha) 原文作者:Bartosz Szczeciński 译文出自:掘金翻译计划 本文永久链接:github.com/xitu/ ...

  8. .htaccess基本语法和应用 (2012-11-09 16:13:47)转载▼

    htaccess基本语法和应用 (2012-11-09 16:13:47) 转载▼ 标签:  htaccess it 分类: 网络 .htaccess是Apache服务器的一个非常强大的分布式配置文件 ...

  9. [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 ...

随机推荐

  1. hdu 5183 Negative and Positive (NP)(STL-集合【HASH】)

    题意: When given an array (a0,a1,a2,⋯an−1) and an integer K, you are expected to judge whether there i ...

  2. 二,zabbix与php的一些问题

    zabbix 检查先决条件 一.php-bcmath 不支持 php 安装 bcmath 扩展(编译安装) PHP的linux版本需要手动安装BCMath扩展,在PHP的源码包中默认包含BCMath的 ...

  3. Hello World之编译链接装载与执行(1)

    一:前言 我打算写一系列博客来说说我对Hello World在计算机中的生命旅程的理解,我是一名软件工程专业的大三学生,有关这个问题我主要的参考书有<深入理解计算机系统>.<现代操作 ...

  4. Git+windows安装教程(一)

    一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以 ...

  5. 攻防世界 Misc 新手练习区 如来十三掌 Writeup

    攻防世界 Misc 新手练习区 如来十三掌 Writeup 题目介绍 题目考点 佛曰加密.base64.Rot13等加密方法的了解 Writeup 下载并打开附件 联想到佛曰加密,复制内容到 佛曰加密 ...

  6. SVD专题1 算子的奇异值分解——矩阵形式的推导

    目录 SVD专题1 算子的奇异值分解--矩阵形式的推导 前言 Preface 几点说明 预备知识 Prerequisite 1.1 极分解 Polar Decomposition 1.2 等距同构 U ...

  7. SQL 添加列,删除列,修改列的类型

    alter table 表名 add 列名 数据类型 如:alter table student add nickname char(20) alter table tableName(表名) add ...

  8. 手把手教你学Dapr - 8. 绑定

    目录 手把手教你学Dapr - 1. .Net开发者的大时代 手把手教你学Dapr - 2. 必须知道的概念 手把手教你学Dapr - 3. 使用Dapr运行第一个.Net程序 手把手教你学Dapr ...

  9. 通过修改host加速访问GitHub

    加速访问GitHub 原理:绕过 DNS 解析,直接使用本地的 DNS 记录进行直接跳转. 可以通过 http://ping.chinaz.com/ 链接查询github的DNS信息,例如,可以直接打 ...

  10. [源码解析] PyTorch分布式(6) -------- DistributedDataParallel -- 初始化&store

    [源码解析] PyTorch分布式(6) ---DistributedDataParallel -- 初始化&store 目录 [源码解析] PyTorch分布式(6) ---Distribu ...