当前位置:网站首页>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
边栏推荐
- LeetCode_ 2 (add two numbers)
- The function of qualifier in C language
- R Language ggplot2 Visualization: visualize linegraph, using Legend in Theme function. Paramètre de position emplacement de la légende personnalisée
- 动态规划
- Two policemen were shot dead in a "safety accident" in Philadelphia, USA
- 729. 我的日程安排表 I :「模拟」&「线段树(动态开点)」&「分块 + 位运算(分桶)」
- How can non-technical departments participate in Devops?
- Faire un clip vidéo auto - média deux fois, comment clip n'est pas considéré comme une infraction
- Redis如何实现多可用区?
- CPU设计相关笔记
猜你喜欢

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

超级哇塞的快排,你值得学会!

Opengauss database source code analysis series articles -- detailed explanation of dense equivalent query technology (Part 2)

FR练习题目---综合题

危机重重下的企业发展,数字化转型到底是不是企业未来救星

网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理

选择排序和冒泡排序

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

Loop invariant

一键更改多个文件名字
随机推荐
Is it OK to open the securities account on the excavation finance? Is it safe?
Disjoint Set
FR练习题目---简单题
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)
【招聘岗位】软件工程师(全栈)- 公共安全方向
Topology visual drawing engine
SaaS multi tenant solution for FMCG industry to build digital marketing competitiveness of the whole industry chain
Implement a blog system -- using template engine technology
There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba
How to protect user privacy without password authentication?
【leetcode周赛总结】LeetCode第 81 场双周赛(6.25)
用 Go 跑的更快:使用 Golang 为机器学习服务
ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)
anaconda使用中科大源
Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
Penetration testing methodology
如何将电脑复制的内容粘贴进MobaXterm?如何复制粘贴
直播预告|如何借助自动化工具落地DevOps(文末福利)
The forked VM terminated without saying properly goodbye
01. Solr7.3.1 deployment and configuration of jetty under win10 platform