xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

React useMemo

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 发布文章使用:只允许注册用户才可以访问!


posted @   xgqfrms  阅读(604)  评论(5编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2018-06-08 css3 var() & auto change skin
2018-06-08 ES6 import json
2018-06-08 google chrome & preferences & languages
2018-06-08 webpack 3.x best practical
2018-06-08 disable VS Code auto format javascript when save codes
2018-06-08 CSS & new class name
2017-06-08 转换 React 为TypeScript
点击右上角即可分享
微信分享提示