当前位置:网站首页>Detailed explanation of usememo, memo, useref and other relevant hooks
Detailed explanation of usememo, memo, useref and other relevant hooks
2022-07-05 14:36:00 【Forgive me for not being free and easy enough】
useMemo
It is mainly used for data calculation , With cache, it will not double calculate ,
amount to vue Of computed
import React,{
useRef, useState, useEffect, useMemo, memo } from 'react'
export default function text() {
const [height, setHeight] = useState(66)
const memoizedCallback = useMemo(
() => {
return height + ' test Callback'
},
[height],
)
return (
<div>{
memoizedCallback}</div>
)
}
memo
memo: Prevent useless updates of subcomponents ( When the whole situation content It will be updated when changes occur ,memo The second parameter can prevent , But the official advice is to use less )
import React,{
useRef, useState, useEffect, useMemo, memo } from 'react'
const Son = function () {
return (
<div className='child-box'>
</div>
)
}
function areEqual(prevProps, nextProps) {
return prevProps === nextProps // You don't want it to update, just go back true
}
const HooksChild = memo(Son, areEqual) // The second parameter may not be passed
export default function text() {
const [height, setHeight] = useState(66)
const memoizedCallback = useMemo(
() => {
return height + ' test Callback'
},
[height],
)
return (
<div>
<HooksChild/>
</div>
)
}
useRef
Provide you with a closed space to store variables , Component re rendering has nothing to do with him , Unless you change it yourself
import {
useRef, useState } from 'react';
const useTimer = (step = 1) => {
const timer = useRef(null)
const [ num, setNum ] = useState(0)
const start = () => {
// const timeout = setInterval(() => {
// setNum((num)=>num + 1)
// }, step * 1000)
timer.current = null
}
const clear = () => {
setNum(0)
clearInterval(timer.current)
}
return {
num,
start,
clear
}
}
export default useTimer
Complete example Customize hooks Encapsulate timers that will not be created repeatedly
useContext,useReducer
useContext Global variables ,useReducer Use and useState The way to use it is similar
Complete example useContext+useReducer Realize global state sharing
边栏推荐
- 区间 - 左闭右开
- How to open an account of qiniu securities? Is it safe to open an account?
- SaaS multi tenant solution for FMCG industry to build digital marketing competitiveness of the whole industry chain
- CPU设计实战-第四章实践任务二用阻塞技术解决相关引发的冲突
- 美国费城发生“安全事故” 2名警察遭枪杀
- R language ggplot2 visual bar graph: visualize the bar graph through the two-color gradient color theme, and add label text for each bar (geom_text function)
- Thymeleaf common functions
- 安装配置Jenkins
- 外盘入金都不是对公转吗,那怎么保障安全?
- R Language ggplot2 Visualization: visualize linegraph, using Legend in Theme function. Paramètre de position emplacement de la légende personnalisée
猜你喜欢

浅谈Dataset和Dataloader在加载数据时如何调用到__getitem__()函数

PHP - fatal error: allowed memory size of 314572800 bytes exhausted

Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment

leetcode:881. 救生艇

安装配置Jenkins

Share 20 strange JS expressions and see how many correct answers you can get

leetcode:881. lifeboat

非技术部门,如何参与 DevOps?

Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading
![[learning notes] stage test 1](/img/22/ad16375d8d1510c2ec75c56403a8bf.png)
[learning notes] stage test 1
随机推荐
安装配置Jenkins
R language ggplot2 visualization: gganimate package is based on Transition_ The time function creates dynamic scatter animation (GIF) and uses shadow_ Mark function adds static scatter diagram as anim
R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram, and use the labs parameter to customize the X axis label text (customize X axis labels)
Explain Vue's plan to clean up keepalive cache in time
启牛证券账户怎么开通,开户安全吗?
Catch all asynchronous artifact completable future
世界环境日 | 周大福用心服务推动减碳环保
非技术部门,如何参与 DevOps?
Security analysis of Web Architecture
注意!软件供应链安全挑战持续升级
Thymeleaf 使用后台自定义工具类处理文本
选择排序和冒泡排序
动态规划
Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
【C 题集】of Ⅷ
729. 我的日程安排表 I :「模拟」&「线段树(动态开点)」&「分块 + 位运算(分桶)」
Topology visual drawing engine
SaaS multi tenant solution for FMCG industry to build digital marketing competitiveness of the whole industry chain
04_ Use of solrj7.3 of solr7.3
【NVMe2.0b 14-9】NVMe SR-IOV