当前位置:网站首页>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使用方法差不多
边栏推荐
- 03_ Dataimport of Solr
- Longest common subsequence dynamic programming
- Implement a blog system -- using template engine technology
- APR protocol and defense
- Use the word "new" to attract curious people
- Sharing the 12 most commonly used regular expressions can solve most of your problems
- C language -- structure and function
- CYCA少儿形体礼仪 宁波市培训成果考核圆满落幕
- How to open an account of qiniu securities? Is it safe to open an account?
- Enjoy what you want. Zhichuang future
猜你喜欢
There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba
Introduction, installation, introduction and detailed introduction to postman!
选择排序和冒泡排序
APR protocol and defense
Redis如何实现多可用区?
有一个强大又好看的,赛过Typora,阿里开发的语雀编辑器
快消品行业SaaS多租户解决方案,构建全产业链数字化营销竞争力
LeetCode_ 2 (add two numbers)
Implement a blog system -- using template engine technology
分享 12 个最常用的正则表达式,能解决你大部分问题
随机推荐
Don't be unconvinced. Mobile phone function upgrade is strong
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)
Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
CYCA少儿形体礼仪 宁波市培训成果考核圆满落幕
There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba
实现一个博客系统----使用模板引擎技术
Lepton 无损压缩原理及性能分析
webRTC SDP mslabel lable
What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
R语言dplyr包select函数、group_by函数、mutate函数、cumsum函数计算dataframe分组数据中指定数值变量的累加值、并生成累加数据列
网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理
强联通分量
Section - left closed right open
How to open an account of qiniu securities? Is it safe to open an account?
黑马程序员-软件测试-10阶段2-linux和数据库-44-57为什么学习数据库,数据库分类关系型数据库的说明Navicat操作数据的说明,Navicat操作数据库连接说明,Navicat的基本使用,
Topology visual drawing engine
无密码身份验证如何保障用户隐私安全?
openGauss数据库源码解析系列文章—— 密态等值查询技术详解(下)
Isn't it right to put money into the external market? How can we ensure safety?
R語言ggplot2可視化:可視化折線圖、使用theme函數中的legend.position參數自定義圖例的比特置