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. Redis 雪崩、穿透和击穿

    https://github.com/doocs/advanced-java/blob/master/docs/high-concurrency/redis-caching-avalanche-and ...

  2. Apache Http Server 一些资料

    配置虚拟主机的例子: http://httpd.apache.org/docs/current/vhosts/examples.html

  3. eclipse项目放到github

    一,下载git ,配置用户名和邮箱: git config --global user.name "name"       git config --global user.ema ...

  4. 精通MySQL之锁篇

    老刘是即将找工作的研究生,自学大数据开发,一路走来,感慨颇深,网上大数据的资料良莠不齐,于是想写一份详细的大数据开发指南.这份指南把大数据的[基础知识][框架分析][源码理解]都用自己的话描述出来,让 ...

  5. mysql数据恢复:.frm和.ibd,恢复表结构和数据

    mysql数据恢复:.frm和.ibd,恢复表结构和数据 一.恢复表结构 二.恢复表数据 相关内容原文地址: CSDN:她说巷尾的樱花开了:mysql根据.frm和.ibd文件恢复表结构和数据 博客园 ...

  6. Geospark-属性字段处理

    Geospark将从shapefile.csv等格式文件以及DataFrame中的读取的字段保存到了Geometry的userData字段中,可以通过调用.getUserData()方法获取,他会返回 ...

  7. HDU6331 Problem M. Walking Plan【Floyd + 矩阵 + 分块】

    HDU6331 Problem M. Walking Plan 题意: 给出一张有\(N\)个点的有向图,有\(q\)次询问,每次询问从\(s\)到\(t\)且最少走\(k\)条边的最短路径是多少 \ ...

  8. Color Changing Sofa Gym - 101962B、Renan and Cirque du Soleil Gym - 101962C、Hat-Xor Gym - 101962E 、Rei do Cangaço Gym - 101962K 、Sorting Machine Gym - 101962M

    Color Changing Sofa Gym - 101962B 题意:给你一个由字母构成的字符串a,再给你一个由0.1构成的字符串b.你需要在a字符串中找到一个可以放下b的位置,要保证b字符串中0 ...

  9. HDU-3499Flight (分层图dijkstra)

    一开始想的并查集(我一定是脑子坏掉了),晚上听学姐讲题才知道就是dijkstra两层: 题意:有一次机会能使一条边的权值变为原来的一半,询问从s到e的最短路. 将dis数组开成二维,第一维表示从源点到 ...

  10. hdu5135 Little Zu Chongzhi's Triangles

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submissi ...