当前位置:网站首页>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
边栏推荐
- 如何将电脑复制的内容粘贴进MobaXterm?如何复制粘贴
- 【NVMe2.0b 14-9】NVMe SR-IOV
- What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
- Is it OK to open the securities account on the excavation finance? Is it safe?
- Thymeleaf 使用后台自定义工具类处理文本
- Thymeleaf th:classappend attribute append th:styleappend style append th:data- custom attribute
- Pointer operation - C language
- 区间 - 左闭右开
- Intelligent supply chain collaboration system solution for daily chemical products industry: digital intelligent SCM supply chain, which is the "acceleration" of enterprise transformation
- Introduction, installation, introduction and detailed introduction to postman!
猜你喜欢
There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba
Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading
Thymeleaf th:with局部变量的使用
有一个强大又好看的,赛过Typora,阿里开发的语雀编辑器
openGauss数据库源码解析系列文章—— 密态等值查询技术详解(下)
乌卡时代下,企业供应链管理体系的应对策略
ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)
直播预告|如何借助自动化工具落地DevOps(文末福利)
[learning notes] stage test 1
随机推荐
The speed monitoring chip based on Bernoulli principle can be used for natural gas pipeline leakage detection
外盘入金都不是对公转吗,那怎么保障安全?
FR练习题目---简单题
分享 20 个稀奇古怪的 JS 表达式,看看你能答对多少
一键更改多个文件名字
面试突击62:group by 有哪些注意事项?
Thymeleaf th:classappend属性追加 th:styleappend样式追加 th:data-自定义属性
R Language ggplot2 Visualization: visualize linegraph, using Legend in Theme function. Paramètre de position emplacement de la légende personnalisée
Sharing the 12 most commonly used regular expressions can solve most of your problems
Thymeleaf 常用函数
C language -- structure and function
CYCA少儿形体礼仪 宁波市培训成果考核圆满落幕
实现一个博客系统----使用模板引擎技术
Two policemen were shot dead in a "safety accident" in Philadelphia, USA
Un week - end heureux
Share 20 strange JS expressions and see how many correct answers you can get
Strong connection component
The forked VM terminated without saying properly goodbye
01. Solr7.3.1 deployment and configuration of jetty under win10 platform
Introduction, installation, introduction and detailed introduction to postman!