当前位置:网站首页>useMemo,memo,useRef等相关hooks详解
useMemo,memo,useRef等相关hooks详解
2022-07-05 14:32:00 【原谅我不够洒脱】
useMemo
主要是用来做数据计算,并带有缓存不会重复计算,
相当于vue的computed
import React,{
useRef, useState, useEffect, useMemo, memo } from 'react'
export default function text() {
const [height, setHeight] = useState(66)
const memoizedCallback = useMemo(
() => {
return height + '测试Callback'
},
[height],
)
return (
<div>{
memoizedCallback}</div>
)
}
memo
memo:防止子组件无用更新(当全局content发生变化时还是会更新,memo第二个参数可以阻止,但是官方建议少用)
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 // 你不想它更新就返回true
}
const HooksChild = memo(Son, areEqual) // 第二个参数可以不传
export default function text() {
const [height, setHeight] = useState(66)
const memoizedCallback = useMemo(
() => {
return height + '测试Callback'
},
[height],
)
return (
<div>
<HooksChild/>
</div>
)
}
useRef
给你提供一个封闭的空间来储存变量,组件重新渲染和他无关,除非你自己去改
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
useContext,useReducer
useContext 全局变量,useReducer使用和useState使用方法差不多
边栏推荐
- 微帧科技荣获全球云计算大会“云鼎奖”!
- CPU设计实战-第四章实践任务二用阻塞技术解决相关引发的冲突
- Total amount analysis accounting method and potential method - allocation analysis
- 启牛证券账户怎么开通,开户安全吗?
- 04_ Use of solrj7.3 of solr7.3
- 直播预告|如何借助自动化工具落地DevOps(文末福利)
- CyCa children's physical etiquette Ningbo training results assessment came to a successful conclusion
- Show strength. In this way, the mobile phone will not be difficult to move forward
- 日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”
- Loop invariant
猜你喜欢

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

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

循环不变式

微帧科技荣获全球云计算大会“云鼎奖”!

Principle and performance analysis of lepton lossless compression

【NVMe2.0b 14-9】NVMe SR-IOV

leetcode:881. 救生艇

FR练习题目---综合题

FR练习题目---简单题

CyCa children's physical etiquette Ningbo training results assessment came to a successful conclusion
随机推荐
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)
矩阵链乘 - 动态规划实例
【招聘岗位】基础设施软件开发人员
注意!软件供应链安全挑战持续升级
be careful! Software supply chain security challenges continue to escalate
SSL证书错误怎么办?浏览器常见SSL证书报错解决办法
[summary of leetcode weekly competition] the 81st fortnight competition of leetcode (6.25)
2022年国内正规的期货公司平台有哪些啊?方正中期怎么样?安全可靠吗?
Countermeasures of enterprise supply chain management system in UCA Era
freesurfer运行完recon-all怎么快速查看有没有报错?——核心命令tail重定向
【招聘岗位】软件工程师(全栈)- 公共安全方向
Shenziyu, the new chairman of Meizu: Mr. Huang Zhang, the founder, will serve as the strategic adviser of Meizu's scientific and technological products
How to open an account of qiniu securities? Is it safe to open an account?
Strong connection component
Is the securities account given by the head teacher of qiniu school safe? Can I open an account?
How to choose the appropriate certificate brand when applying for code signing certificate?
Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用shadow_mark函数为动画添加静态散点图作为动画背景
Postgresql 13 安装
【学习笔记】图的连通性与回路