Effect Hook
1 数据获取,设置订阅以及手动更改 React 组件中的 DOM 都属于副作用。
2 可以把 useEffect Hook 看做 componentDidMount,componentDidUpdate 和 componentWillUnmount 这三个函数的组合。
3 useEffect函数第二个参数是可选的:
如果不传,则每次渲染都执行副作用;
如果传入数组,则数组的成员变化了(比较引用地址),才执行副作用。
一 不需要清除的副作用
import React, { useState, useEffect } from "react";
// 记录渲染次数
let times = 0;
export default function App(props) {
console.log(`第${++times}次渲染`);
const [counter, setCounter] = useState(0);
useEffect(() => {
document.title = `第${counter}次点击`;
});
return (
<div>
<button onClick={e => setCounter(counter + 1)}>按钮</button>
</div>
);
}
二 需要清除的副作用
import React, { useState, useEffect } from "react";
export default function App(props) {
const [size, setSize] = useState({
width: window.innerWidth,
height: window.innerHeight
});
useEffect(() => {
window.addEventListener("resize", onResize, false);
return () => {
window.removeEventListener("resize", onResize, false);
};
}, []);
const onResize = e => {
setSize({
width: window.innerWidth,
height: window.innerHeight
});
};
return (
<div>
窗口尺寸:{size.width}*{size.height}
</div>
);
}
Effect Hook的更多相关文章
- [React] Write a Custom React Effect Hook
Similar to writing a custom State Hook, we’ll write our own Effect Hook called useStarWarsQuote, whi ...
- [React] Use the React Effect Hook in Function Components
Similar to the State Hook, the Effect Hook is “first-class” in React and handy for performing side e ...
- 【React 资料备份】React Hook
Hooks是React16.8一个新增项,是我们可以不用创建class组件就能使用状态和其他React特性 准备工作 升级react.react-dom npm i react react-dom - ...
- react新特性hook
一.hook示例. import React, { useState } from 'react'; function Example() { // 声明一个叫 “count” 的 state 变 ...
- React Hook:使用 useEffect
React Hook:使用 useEffect 一.描述 二.需要清理的副作用 1.在 class 组件中 2.使用 effect Hook 的示例 1.useEffect 做了什么? 2.为什么在组 ...
- React Hook上车
React Hook 是 v16.8 的新功能,自诞生以来,受到广泛的好评,在 React 版本更新中具有里程碑的意义.现在都2020年了,再不上车 React Hook 就真的 out 了... H ...
- [React]Hook初探
Hook是什么 Hook是React从16.8开始支持的特性,使用Hook可以在不使用class时使用state Hook支持在不需要修改组件状态的情况下复用逻辑状态,从而解决使用render pro ...
- React使用hook
Hook 是 React 16.8 的新增特性.它可以让你在不编写 class 的情况下使用 state 以及其他的 React 特性. 为什么会有hook 在组件之间复用状态逻辑很难,需要重新组织你 ...
- React Hooks (React v16.7.0-alpha)
:first-child{margin-top:0!important}.markdown-body>:last-child{margin-bottom:0!important}.markdow ...
随机推荐
- Thinkphp 获取最大值id值
有时候项目需要获取数据库最大的id值,比如生成订单,做排序号,那么Thinkphp 如何获取最大值id值. $info=D('Customer')->where('1=1')->order ...
- R语言基础篇——数据对象
1.基本数据类型(numeric,logical,character,NA,double,complex,integer) 2.日期变量 常用函数 Sys.Date()-返回系统当前的日期,Sys.t ...
- Ribbon远程调用
Ribbon是客户端的负载均衡机制,它有几种负载均衡机制.默认是轮询,我们也可以自定义规则.通过合理的分配网络请求来减小服务器的压力.项目都是注册到eureka服务器上.通过ribbon去调用其他服务 ...
- 09-js定时器、函数
# js定时器 通过使用 JavaScript,我们有能力作到在一个设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行.我们称之为计时事件. **定时器在javascript中的作用** 1. ...
- Google authenticator 谷歌身份验证,实现动态口令
Google authenticator 谷歌身份验证,实现动态口令 google authenticator php 服务端 使用PHP类 require_once '../PHPGangsta/G ...
- C# WinForm定时触发事件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Perl脚本通过Expect登陆多台设备批量执行命令并Log
本例子尝试使用Perl脚本借助Expect模块实现如下目的: 登陆多台设备 设备登陆信息按如下格式存放于文件中. $ cat hosts.txt 192.168.30.7:node1:telnet:b ...
- Java使用多线程发送消息
在后台管理用户信息的时候,经常会用到批量发送提醒消息,首先想到的有: (1).循环发送列表,逐条发送.优点是:简单,如果发送列表很少,而且没有什么耗时的操作,是比较好的一种选择,缺点是:针对大批量的发 ...
- css 块级格式化上下文(BFC)
一.块级格式化上下文(BFC) 1.什么是块级格式化上下文? Block Formatting Contexts (BFC,块级格式化上下文)就是一个块级元素 的渲染显示规则 (可以把 BFC 理解为 ...
- [洛谷P3205] HNOI2010 合唱队
问题描述 为了在即将到来的晚会上有更好的演出效果,作为AAA合唱队负责人的小A需要将合唱队的人根据他们的身高排出一个队形.假定合唱队一共N个人,第i个人的身高为Hi米(1000<=Hi<= ...