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. centos解决Could not find a version that satisfies the requirement pip3 (from versions: none)及No matching distribution found for pip3问题

    python环境:python 3.8 报错信息: WARNING: pip is configured with locations that require TLS/SSL, however th ...

  2. 看漫画就能学SQL,简直太cool了

    对于SQl, 很多人学不会的原因是从一开始就没明白,学这东西能干啥,学会了能有什么用.甚至有些人不知道'SQL'应该怎么读,以至于一开始兴致勃勃,但是学到一半放弃了. 注意:'sql'真的不能读成'烧 ...

  3. asp.net core2.1认证和授权解密

    来源:https://www.cnblogs.com/pangjianxin/p/9372562.html asp.net core2.1认证和授权解密 本篇文章翻译自:https://digital ...

  4. NIO的缓冲区、通道、选择器关系理解

    Buffer的数据存取    一个用于特定基本数据类行的容器.有java.nio包定义的,所有缓冲区都是抽象类Buffer的子类.   Java NIO中的Buffer主要用于与NIO通道进行交互,数 ...

  5. hibernate.hbm.xml配置文件解析

    转自:https://www.cnblogs.com/uoar/p/6670612.html 1. <!DOCTYPE hibernate-mapping PUBLIC "-//Hib ...

  6. 2018-12-25-win10-uwp-显示SVG

    title author date CreateTime categories win10 uwp 显示SVG lindexi 2018-12-25 10:37:5 +0800 2018-2-13 1 ...

  7. man(2) W

    wait(2) wait3(2) wait4(2) waitpid(2) waitid(2) SEE ALSO _exit(2), clone(2), fork(2), kill(2), ptrace ...

  8. 【学习】005 线程池原理分析&锁的深度化

    线程池 什么是线程池 Java中的线程池是运用场景最多的并发框架,几乎所有需要异步或并发执行任务的程序 都可以使用线程池.在开发过程中,合理地使用线程池能够带来3个好处. 第一:降低资源消耗.通过重复 ...

  9. kettle 通过JDBC 连接SQL Server(Error occurred while trying to connect to the database)

    在连接数据(MS SQLServer 2008)发现:Error occurred while trying to connect to the database 然后找资料看,都不是问题所在,最后一 ...

  10. 对webpack的初步研究1

    一.概念: 1.webpack的核心是用于现代JavaScript应用程序的静态模块捆绑器.当webpack处理您的应用程序时,它会在内部构建一个依赖关系图,它映射您的项目所需的每个模块并生成一个或多 ...