当前位置:网站首页>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
边栏推荐
- 最长公共子序列 - 动态规划
- 不相交集
- 动态规划
- APR protocol and defense
- Share 20 strange JS expressions and see how many correct answers you can get
- 【leetcode周赛总结】LeetCode第 81 场双周赛(6.25)
- 04_ Use of solrj7.3 of solr7.3
- There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba
- Is it OK to open the securities account on the excavation finance? Is it safe?
- 有一个强大又好看的,赛过Typora,阿里开发的语雀编辑器
猜你喜欢

两个BI开发,3000多张报表?如何做的到?

CyCa children's physical etiquette Ningbo training results assessment came to a successful conclusion

申请代码签名证书时如何选择合适的证书品牌?

Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!

How does redis implement multiple zones?

选择排序和冒泡排序

Thymeleaf th:classappend attribute append th:styleappend style append th:data- custom attribute

There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba

分享 12 个最常用的正则表达式,能解决你大部分问题

Thymeleaf 使用后台自定义工具类处理文本
随机推荐
Strong connection component
Topology可视化绘图引擎
js亮瞎你眼的日期选择器
C语言中限定符的作用
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
[summary of leetcode weekly competition] the 81st fortnight competition of leetcode (6.25)
Time to calculate cron expression based on cronsequencegenerator
网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理
How to make a second clip of our media video without infringement
SSL证书错误怎么办?浏览器常见SSL证书报错解决办法
裁员下的上海
快消品行业SaaS多租户解决方案,构建全产业链数字化营销竞争力
Qingda KeYue rushes to the science and Innovation Board: the annual revenue is 200million, and it is proposed to raise 750million
浅谈Dataset和Dataloader在加载数据时如何调用到__getitem__()函数
APR protocol and defense
基于TI DRV10970驱动直流无刷电机
Sharing the 12 most commonly used regular expressions can solve most of your problems
详解Vue适时清理keepalive缓存方案
做自媒体视频二次剪辑,怎样剪辑不算侵权
R語言ggplot2可視化:可視化折線圖、使用theme函數中的legend.position參數自定義圖例的比特置