当前位置:网站首页>Useeffect, triggered when function components are mounted and unloaded
Useeffect, triggered when function components are mounted and unloaded
2022-07-06 16:14:00 【Dream height 1.8 meters】
1 With no arguments
It is executed after the first rendering and after each update
<html>
<head>
<script src="https://unpkg.com/[email protected]/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
</body>
<script type="text/babel"> const e = React.createElement; const custom = () => {
const [number, setNumber] = React.useState(0); React.useEffect(() => {
console.log(' Trigger useEffect' + number); }); return <button onClick={
() => setNumber(number + 1)}> Click on {
number}</button> } const domContainer = document.querySelector('#root'); ReactDOM.render(e(custom), domContainer); </script>
</html>
Each update , Will trigger useEffect
2 With parameters
You can send more than one [param1,param2,param3....]
Execute when the specified parameter changes
<html>
<head>
<script src="https://unpkg.com/[email protected]/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
</body>
<script type="text/babel"> const e = React.createElement; const custom = () => {
const [number, setNumber] = React.useState(0); const [change, setChange] = React.useState(0); React.useEffect(() => {
console.log(' Trigger useEffect' + number); }, [change]); return [<button onClick={
() => setNumber(number + 1)}>number{
number}</button>, <button onClick={
() => setChange(change + 1)}>change{
change}</button>] } const domContainer = document.querySelector('#root'); ReactDOM.render(e(custom), domContainer); </script>
</html>
Only change When it's updated , Will trigger useEffect
3 Pass in empty array
Run only once effect( Only when the component is mounted and uninstalled )
<html>
<head>
<script src="https://unpkg.com/[email protected]/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
</body>
<script type="text/babel"> const e = React.createElement; const custom = () => {
const [number, setNumber] = React.useState(0); React.useEffect(() => {
console.log(' Trigger useEffect' + number); setNumber(5) }, []); return <button onClick={
() => setNumber(number + 1)}> Click on {
number}</button> } const domContainer = document.querySelector('#root'); ReactDOM.render(e(custom), domContainer); </script>
</html>
( The beginning is refreshed ) When you mount it useEffect hold number Set to 5, Subsequent updates do not trigger useEffect
4 effect Return function
Execute the return function when uninstalling
Take the timer as an example , This is the case when there is no return function
<html>
<head>
<script src="https://unpkg.com/[email protected]/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
</body>
<script type="text/babel"> const e = React.createElement; const Custom = () => {
const [number, setNumber] = React.useState(0); React.useEffect(() => {
console.log(' Trigger useEffect' + number) let interval = null; let t = 0 interval = setInterval(() => {
console.log(' Timer ', t); setNumber(++t) }, 1000); }, []); return <div> Timer {
number}</div> } const customSwitch = () => {
const [on, setOn] = React.useState(false); return [<button onClick={
() => setOn(!on)}>{
on ? ' close ' : ' Turn on '}</button>, on && <Custom />] } const domContainer = document.querySelector('#root'); ReactDOM.render(e(customSwitch), domContainer); </script>
</html>
custom When the component is uninstalled , The timer did not cancel , After remounting , There are two timers
Cancel the timer in the return function
<html>
<head>
<script src="https://unpkg.com/[email protected]/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
</body>
<script type="text/babel"> const e = React.createElement; let interval = null; const Custom = () => {
const [number, setNumber] = React.useState(0); React.useEffect(() => {
console.log(' Trigger useEffect' + number) let t = 0 interval = setInterval(() => {
console.log(' Timer ', t); setNumber(++t) }, 1000); return ()=> {
console.log(' Trigger when unloading effect') clearInterval(interval) } }, []); return <div> Timer {
number}</div> } const customSwitch = () => {
const [on, setOn] = React.useState(false); return [<button onClick={
() => setOn(!on)}>{
on ? ' close ' : ' Turn on '}</button>, on && <Custom />] } const domContainer = document.querySelector('#root'); ReactDOM.render(e(customSwitch), domContainer); </script>
</html>
uninstall custom When , Cancel timer , When you mount again , Turn on timer .
Official website :https://react.docschina.org/docs/hooks-effect.html
This is also very good :https://blog.csdn.net/aiwwy/article/details/123338490
边栏推荐
- Perform general operations on iptables
- Sword finger offer II 019 Delete at most one character to get a palindrome
- Advancedinstaller安装包自定义操作打开文件
- 计算时间差
- Quick to typescript Guide
- 1005. Maximized array sum after K negations
- 滲透測試 ( 1 ) --- 必備 工具、導航
- Web based photo digital printing website
- 1689. Ten - the minimum number of binary numbers
- 875. 爱吃香蕉的珂珂 - 力扣(LeetCode)
猜你喜欢
(POJ - 3579) median (two points)
分享一个在树莓派运行dash应用的实例。
Penetration test (1) -- necessary tools, navigation
Candy delivery (Mathematics)
The "sneaky" new asteroid will pass the earth safely this week: how to watch it
605. Planting flowers
Flask框架配置loguru日志库
QT实现窗口渐变消失QPropertyAnimation+进度条
Flag framework configures loguru logstore
Penetration test (2) -- penetration test system, target, GoogleHacking, Kali tool
随机推荐
Openwrt build Hello ipk
树莓派4B64位系统安装miniconda(折腾了几天终于解决)
Radar equipment (greedy)
Luogu P1102 A-B number pair (dichotomy, map, double pointer)
[exercise-1] (UVA 673) parentheses balance/ balanced brackets (stack)
[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class
Quick to typescript Guide
渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集
Raspberry pie csi/usb camera uses mjpg to realize web camera monitoring
[exercise -10] unread messages
1903. Maximum odd number in string
Suffix expression (greed + thinking)
Write web games in C language
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
If you want to apply for a programmer, your resume should be written like this [essence summary]
F - birthday cake (Shandong race)
Read and save zarr files
Common configuration files of SSM framework
B - Code Party (girls' competition)
Nodejs+vue online fresh flower shop sales information system express+mysql