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 ...
随机推荐
- python之设置windows背景图片
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'jiangwenwen' from PIL import Image impo ...
- ES6扩展运算符(三点符号), 解构
http://www.cnblogs.com/chrischjh/p/4848934.html
- Mysql 数据库存储的原理??
储存过程是一个可编程的函数,它在数据库中创建并保存.它可以有 SQL 语句和一些特殊的控制结 构组成.当希望在不同的应用程序或平台上执行相同的函数,或者封装特定功能时,存储过程是非常有用的.数据库中的 ...
- .NET基础篇——利用泛型与反射更新实体(ADO.NET Entity Framework)(转)
自从ADO.NET Entity Framework面世以来,受到大家的热捧,它封装了大量代码生成的工具,用户只需要建立好实体之间的关系,系统就是会为用户自动成功了Add.Delete.CreateO ...
- Latex--入门系列二
Latex 专业的参考 tex对于论文写作或者其他的一些需要拍版的写作来说,还是非常有意义的.我在网上看到这个对于Latex的入门介绍还是比较全面的,Arbitrary reference.所以将会翻 ...
- ][mybatis]MyBatis mapper文件中的变量引用方式#{}与${}的差别
转自https://blog.csdn.net/szwangdf/article/details/26714603 MyBatis mapper文件中的变量引用方式#{}与${}的差别 默认情况下,使 ...
- 【记录】git error:bad signature 解决方法
今天提交git 的时候出现 bad signature 错误,意思是git下的index文件损坏了,需要重新生成下 error: bad signature fatal: index file cor ...
- Linux系统启动过程浅析
经过老师的讲解以及查阅资料后,现对Linux系统启动做以浅析,仅是个人理解. 主要的步骤有以下几步: 第一步:Power On.用户按下电源开关的那一瞬间,叫Power On阶段 .在这个阶段,BIO ...
- KC705E 增强版 基于FMC接口的Xilinx Kintex-7 FPGA K7 XC7K325T PCIeX8 接口卡
KC705E 增强版 基于FMC接口的Xilinx Kintex-7 FPGA K7 XC7K325T PCIeX8 接口卡 一.板卡概述 本板卡基于Xilinx公司的FPGAXC7K325T-2FF ...
- qt 视频播放
可以播出视频,待完善 player = new QMediaPlayer(this); playlist = new QMediaPlaylist(); playlist->addMedia(Q ...