React 16.13.1触发两次render
一段很普通的代码,出发了两次render
import React, { useState, useEffect } from 'react'
const MouseTracker: React.FC = () => {
const [ positions, setPositions ] = useState({x: 0, y: 0})
useEffect(() => {
console.log('add effect', positions.x)
const updateMouse= (e: MouseEvent) => {
console.log('inner')
setPositions({ x: e.clientX, y: e.clientY })
}
document.addEventListener('click', updateMouse)
return () => {
console.log('remove effect', positions.x)
document.removeEventListener('click', updateMouse)
}
},[])
console.log('before render', positions.x) // 执行了两次
return (
<>
<p>X: {positions.x}, Y : {positions.y}</p>
</>
)
}
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的更多相关文章
- [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 ...
- [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 ...
- React 16 加载性能优化指南
关于 React 应用加载的优化,其实网上类似的文章已经有太多太多了,随便一搜就是一堆,已经成为了一个老生常谈的问题. 但随着 React 16 和 Webpack 4.0 的发布,很多过去的优化手段 ...
- 盘点 React 16.0 ~ 16.5 主要更新及其应用
目录 0. 生命周期函数的更新 1. 全新的 Content API 2. React Strict Mode 3. Portal 4. Refs 5. Fragment 6. 其他 7. 总结 生命 ...
- Facebook发布React 16 专利条款改为MIT开源协议
9 月 26 日,用于构建 UI 的 JavaScript 库 React 16 的最新版本上线. Facebook 最终在现有的两种 React 版本中选择了出现 bug 概率最少的一款.这次版本更 ...
- react 16 ssr的重构踩坑
ssr 服务端不能识别前端的window.特别是首屏渲染的数据需要用到window对象(比如href += location.search); 服务端不能加载图片,css文件. require.ext ...
- [译] React 16.3(.0-alpha) 新特性
原文地址:What's new in React 16.3(.0-alpha) 原文作者:Bartosz Szczeciński 译文出自:掘金翻译计划 本文永久链接:github.com/xitu/ ...
- .htaccess基本语法和应用 (2012-11-09 16:13:47)转载▼
htaccess基本语法和应用 (2012-11-09 16:13:47) 转载▼ 标签: htaccess it 分类: 网络 .htaccess是Apache服务器的一个非常强大的分布式配置文件 ...
- [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 ...
随机推荐
- JMeter跨线程组保持登录(多线程组共享cookie)
使用__setProperty设置全局变量: 1.jmeter中创建一个登录请求,然后执行,察看结果树-->查看返回cookie信息,我的是在Response data中的 Response h ...
- DockerFile-构建容器的基石
DockerFile 非常的关键,它不同于 docker commit 的手动命令方式来进行镜像的构建和修改,类似 docker commit 的交互被称为命令式交互.命令式交互是运维一直绕不开的一种 ...
- 《手把手教你》系列技巧篇(四十一)-java+ selenium自动化测试 - 处理iframe -上篇(详解教程)
1.简介 原估计宏哥这里就不对iframe这个知识点做介绍和讲解了,因为前边的窗口切换就为这种网页处理提供了思路,另一个原因就是虽然iframe很强大,但是现在很少有网站用它了.但是还是有小伙伴或者童 ...
- LeetCode刷题 字符串详解
一.字符串常用的操作 1. string类 1.1 string的定义与初始化 1.1.1 string的定义 1.1.2 string的初始化 1.2 string的赋值与swap.大小操作.关系运 ...
- Rancher 下图形界面 搭建 K8S 集群
首先我们准备4台 2核3G 的 centos 7 温馨提示:先安装好一台 CentOS 的虚拟机,并且安装好 docker,永久关闭防火墙. 再这个基础上我们分别克隆出四台 Rancher.K8S1. ...
- git修改用户和邮箱
GIT查看当前用户以及邮箱 $ git config user.name $ git config user.email GIT修改用户以及邮箱 $ git config --global user. ...
- layui 如果调用 from 内嵌入的 iframe子页面方法
(人笨,占时想法的办法,不要骂,不要骂,怕了怕了,想到别的会来改的) 父页面; <%@ page language="java" contentType="text ...
- Salesforce Consumer Goods Cloud 浅谈篇四之店内拜访的创建和执行
本篇参考: https://v.qq.com/x/page/f0772toebhd.html https://v.qq.com/x/page/e0772tsmtek.html https://v.qq ...
- 细说ThreadLocal(一)
前言 java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域.如下图所示: 其中堆是占虚拟机中内存最大的,堆被所有线程所共享,其最主要的便是存放实例对象.也因为堆内存是共 ...
- JAVA基础----面向对象复习和IDEA的安装和使用
1.使用集成开发工具eclipse 1.1.java的集成开发工具很多,包括:eclipse.Intellij IDEA.netbeans..... eclipse: IBM开发的.eclipse翻译 ...