当前位置:网站首页>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
边栏推荐
- 有一个强大又好看的,赛过Typora,阿里开发的语雀编辑器
- 外盘入金都不是对公转吗,那怎么保障安全?
- Catch all asynchronous artifact completable future
- 注意!软件供应链安全挑战持续升级
- LeetCode_ 69 (square root of x)
- Introduction, installation, introduction and detailed introduction to postman!
- mysql8.0JSON_CONTAINS的使用说明
- R language ggplot2 visual density map: Visual density map by group and custom configuration geom_ The alpha parameter in the density function sets the image transparency (to prevent multiple density c
- APR protocol and defense
- Mysql database installation tutorial under Linux
猜你喜欢
随机推荐
申请代码签名证书时如何选择合适的证书品牌?
循环不变式
dynamic programming
Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading
家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级
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)
Thymeleaf th:classappend attribute append th:styleappend style append th:data- custom attribute
729. 我的日程安排表 I :「模拟」&「线段树(动态开点)」&「分块 + 位运算(分桶)」
【NVMe2.0b 14-9】NVMe SR-IOV
CYCA少儿形体礼仪 宁波市培训成果考核圆满落幕
微帧科技荣获全球云计算大会“云鼎奖”!
在Pytorch中使用Tensorboard可视化训练过程
mysql8.0JSON_ Instructions for using contains
Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!
Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]
分享 20 个稀奇古怪的 JS 表达式,看看你能答对多少
Thymeleaf 使用后台自定义工具类处理文本
R language uses boxplot function in native package (basic import package, graphics) to visualize box plot
Total amount analysis accounting method and potential method - allocation analysis
Share 20 strange JS expressions and see how many correct answers you can get