React useMemo

react hooks

https://reactjs.org/docs/hooks-reference.html#usememo



useCallback & useMemo

the difference is that useCallback returns a memoized callback and useMemo returns a memoized value

https://flaviocopes.com/react-hook-usememo/


import React, { useMemo } from 'react'; // cache value
const memoizedValue = useMemo(() => expensiveOperation()); const memoizedValue = useMemo(() => expensiveOperation(param1, param2), [param1, param2])

import React, { useCallback } from 'react'; const memoizedCallback = useCallback(
() => {
doSomething(a, b);
},
[a, b],
);

refs

https://medium.com/javascript-in-plain-english/react-usememo-and-when-you-should-use-it-e69a106bbb02

https://kentcdodds.com/blog/usememo-and-usecallback

https://www.digitalocean.com/community/tutorials/react-usememo

https://usehooks.com/useMemo/

https://jancat.github.io/post/2019/translation-usememo-and-usecallback/

https://www.youtube.com/watch?v=RkBg0gDTLU8

useFetch


"use strict"; /**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-06-08
* @modified
*
* @description useFetch 自定义 react hooks
* @augments
* @example
* @link
*
*/ import {
useEffect,
useState,
useRef,
} from "react"; const log = console.log; const useFetch = (url = ``) => {
const isCurrent = useRef(true);
const [state, setState] = useState({
data: null,
loading: true,
});
useEffect(() => {
return () => {
// component unmount
isCurrent.current = false;
}
}, []);
useEffect(() => {
setState(state => ({
data: state.date,
loading: true,
}));
fetch(url)
.then(res => res.json())
.then(json => {
log(`json`, json)
if(isCurrent.current) {
setState({
data: json.date,
loading: false,
})
}
})
return () => {
cleanup
}
}, [url, setState]);
return state;
} export default useFetch; export {
useFetch,
};

https://github.com/benawad/react-hooks-tutorial/blob/5_useMemo/src/useFetch.js

refs



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


React useMemo的更多相关文章

  1. [React] Always useMemo your context value

    Have a similar post about Reac.memo. This blog is the take away from this post. To understand why to ...

  2. React Hooks 深入系列 —— 设计模式

    本文是 React Hooks 深入系列的后续.此篇详细介绍了 Hooks 相对 class 的优势所在, 并介绍了相关 api 的设计思想, 同时对 Hooks 如何对齐 class 的生命周期钩子 ...

  3. 手写一个React-Redux,玩转React的Context API

    上一篇文章我们手写了一个Redux,但是单纯的Redux只是一个状态机,是没有UI呈现的,所以一般我们使用的时候都会配合一个UI库,比如在React中使用Redux就会用到React-Redux这个库 ...

  4. React报错之React Hook useEffect has a missing dependency

    正文从这开始~ 总览 当useEffect钩子使用了一个我们没有包含在其依赖数组中的变量或函数时,会产生"React Hook useEffect has a missing depende ...

  5. 精读《Function Component 入门》

    1. 引言 如果你在使用 React 16,可以尝试 Function Component 风格,享受更大的灵活性.但在尝试之前,最好先阅读本文,对 Function Component 的思维模式有 ...

  6. 干货满满-原来这才是hooks-React Hooks使用心得

    序言 ---最后有招聘信息哦-React是一个库,它不是一个框架.用于构建用户界面的Javascript库.这里大家需要认识这一点.react的核心在于它仅仅是考虑了如何将dom节点更快更好更合适的渲 ...

  7. useMemo优化React Hooks程序性能(九)

    useMemo主要用来解决使用React hooks产生的无用渲染的性能问题.使用function的形式来声明组件,失去了shouldCompnentUpdate(在组件更新之前)这个生命周期,也就是 ...

  8. React Hooks: useMemo All In One

    React Hooks: useMemo All In One useMemo https://reactjs.org/docs/hooks-reference.html#usememo refs x ...

  9. React Hooks & useCallback & useMemo

    React Hooks & useCallback & useMemo https://reactjs.org/docs/hooks-reference.html#usecallbac ...

随机推荐

  1. JPEG解码——(3)文件头解析

    与具体的编码数据空间相比,jpeg文件头占据非常小乃至可以忽略不计的大小. 仍然拿JPEG解码--(1)JPEG文件格式概览中的<animal park>这张图片来举例,从跳过SOS(FF ...

  2. docker基本使用-常用命令

    一. 常用命令 #查看docker服务 docker ps #启动docker服务 systemctl start docker #查看本地镜像 docker images #删除本地镜像 docke ...

  3. Redis-设置Key的过期时间及相关策略

    Redis-设置Key的过期时间及相关策略 1.设置key的过期时间 1.1expire key second:设置key的过期时间(秒) 1.2ttl key:查看key的有效期 1.3persis ...

  4. Spark SQL 自定义函数类型

    Spark SQL 自定义函数类型 一.spark读取数据 二.自定义函数结构 三.附上长长的各种pom 一.spark读取数据 前段时间一直在研究GeoMesa下的Spark JTS,Spark J ...

  5. toggle() 隐藏和收缩

    <!DOCTYPE html><html><head><script src="/jquery/jquery-1.11.1.min.js" ...

  6. WPF 一种带有多个子集的类ComBox 解决方法

    在最近的工作中遇到很多,类似这种layUI风格的Combox: 因为WPF原本的控件,并不具备这种功能,尝试重写Combox的模板,发现无从下手. 于是尝试从多个控件组合来实现这个功能. 这里使用了P ...

  7. Dubbo官网实战使用技巧

    原文链接:Dubbo官网实战使用技巧 1.启动时检查: 我们检查依赖的服务是否启动,可利用下面三个属性,优先级从左到右逐渐降低. 如果服务不是强依赖,或者说服务之间可能存在死循环依赖,我们应该将 ch ...

  8. Java二维数组转成稀疏sparsearray数组

    稀疏数组 基本介绍 当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组. 稀疏数组的处理方法是: 记录数组一共有几行几列,有多少个不同的值 把具有不同值的元素的行列及值记 ...

  9. Educational Codeforces Round 91 (Rated for Div. 2) C. Create The Teams

    题目链接:https://codeforces.com/contest/1380/problem/C 题意 给 $n$ 个数分组,要求每组的最小值乘以该组数的个数不小于 $x$ . 题解 从大到小依次 ...

  10. zjnu1725 COCI (类似二维树状数组模拟)

    Description The 3rd round of COCI is already here! In order to bet on predict the scores, we have as ...