1 数据获取,设置订阅以及手动更改 React 组件中的 DOM 都属于副作用。

2 可以把 useEffect Hook 看做 componentDidMountcomponentDidUpdate 和 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的更多相关文章

  1. [React] Write a Custom React Effect Hook

    Similar to writing a custom State Hook, we’ll write our own Effect Hook called useStarWarsQuote, whi ...

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

  3. 【React 资料备份】React Hook

    Hooks是React16.8一个新增项,是我们可以不用创建class组件就能使用状态和其他React特性 准备工作 升级react.react-dom npm i react react-dom - ...

  4. react新特性hook

    一.hook示例.   import React, { useState } from 'react'; function Example() { // 声明一个叫 “count” 的 state 变 ...

  5. React Hook:使用 useEffect

    React Hook:使用 useEffect 一.描述 二.需要清理的副作用 1.在 class 组件中 2.使用 effect Hook 的示例 1.useEffect 做了什么? 2.为什么在组 ...

  6. React Hook上车

    React Hook 是 v16.8 的新功能,自诞生以来,受到广泛的好评,在 React 版本更新中具有里程碑的意义.现在都2020年了,再不上车 React Hook 就真的 out 了... H ...

  7. [React]Hook初探

    Hook是什么 Hook是React从16.8开始支持的特性,使用Hook可以在不使用class时使用state Hook支持在不需要修改组件状态的情况下复用逻辑状态,从而解决使用render pro ...

  8. React使用hook

    Hook 是 React 16.8 的新增特性.它可以让你在不编写 class 的情况下使用 state 以及其他的 React 特性. 为什么会有hook 在组件之间复用状态逻辑很难,需要重新组织你 ...

  9. React Hooks (React v16.7.0-alpha)

    :first-child{margin-top:0!important}.markdown-body>:last-child{margin-bottom:0!important}.markdow ...

随机推荐

  1. POJ-3468 A Simple Problem with Integers (区间求和,成段加减)

    You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...

  2. JEECG 深度使用培训班 周六周日公开课(一期班)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhangdaiscott/article/details/25411023 广大技术爱好者:     ...

  3. 08-js流程控制、循环、元素操作

    # js流程控制 > 流程控制用于基于不同的条件来执行不同的动作. ### if语句 >if... else ... >if ... else if ... else... > ...

  4. Sql Server 2008安装时提示重启计算机失败解决办法

    在键盘上按下组合键[Win]+[R],调出运行窗口.   在窗口中输入“regedit”,点击确定,打开注册表管理界面.   在注册表左侧目录栏中找到如下位置:“HKEY_LOCAL_MACHINE\ ...

  5. WPF自定义样式篇-DataGrid

    WPF自定义样式篇-DataGrid 先上效果图: 样式:  <!--DataGrid样式-->    <Style TargetType="DataGrid"& ...

  6. iis8.5部署net项目

    服务器上运行提示: iis版本: 使用命令行运行cmd(管理员模式) 输入: %windir%\system32\inetsrv\appcmd unlock config -section:syste ...

  7. 修改 linux 默认字符集

    [root@eric6 ~]# cat /etc/sysconfig/i18n //查看 linux 默认的字符集,默认是 UTF-8 LANG="zh_CN.UTF-8" cp ...

  8. 求助:关于shell数值比较的错误提示

    今天写了个脚本,过不了错误这一关,求大神路过瞟一眼. 1 #!/bin/bash 2 #disk use 3 disk_use() { 4 DISK_LOG=/tmp/disk_use.tmp 5 D ...

  9. 基于cdn方式的vue+element-ui的单页面架构

    一.下载vue2.x,下载element-ui.js以及css 二.html文件 <!DOCTYPE html> <html> <head> <meta ch ...

  10. centos 搭建svn服务器

    1 安装svnserve yum install subversion -y 2 创建仓库 mkdir /svn/rep1 -p mkdir /svn/rep2 -p svnadmin create ...