mikumikugo

博客园 首页 新随笔 联系 订阅 管理

useMemo()和useCallback()都能够起到缓存的作用,只不过useMemo()针对于数据,useCallback()针对于函数

useMemo()

不使用useMemo()

当点击按钮时会改变value的值,同时也会一直执行console.log(1),即take函数一直在被调用

import React, { useState, useMemo } from 'react'

export default function UseMemo() {
  const [value, setvalue] = useState(0)
  const [count, setcount] = useState(1)
  const take = (count) => {
    console.log(1)
    return count * 3
  }
  const price = take(count)

  return (
    <div>
      <p>value:{value}   count:{price}</p>
      <button onClick={() => setvalue(value + 1)}>value+1</button>
    </div>
  )
}

使用useMemo

使用useMemo()包裹后,点击按钮不会引起take函数的执行,price的值始终都是从缓存里面拿到的,只有在后面的依赖里面添加了依赖,且依赖的值改变了,才会执行被包裹的代码,对性能有一定的提升

import React, { useState, useMemo } from 'react'

export default function UseMemo() {
  const [value, setvalue] = useState(0)
  const [count, setcount] = useState(1)
  const take = (count) => {
    console.log(1)
    return count * 3
  }
  const price = useMemo(() => {
    return take(count)
  }, [])
  return (
    <div>
      <p>value:{value}   count:{price}</p>
      <button onClick={() => setvalue(value + 1)}>value+1</button>
    </div>
  )
}

useCallback()

不使用useCallback()的情况,每次组件更新,函数都会重新创建,此时每点击一次按钮,score会加1,同时可以观察到控制台输出的值也是增加的

App.js

import logo from './logo.svg';
import { useState, useCallback } from 'react';
import './App.css';
// import UseMemo from './hooks/03-useMemo'
import UseCallback from './hooks/04-useCallback';


function App() {
  const [score, setscore] = useState(0)
  const [money, setmoney] = useState(1)
  // const showScore = useCallback(() => {
  //   console.log(money)
  //   console.log(score)
  // }, [money])

  const showScore = () => {
    console.log(money)
    console.log(score)
  }


  return (
    <div className="App">

      <UseCallback test={test} setscore={setscore} score={score} money={money} showScore={showScore} setmoney={setmoney} />
    </div>
  );
}

export default App;

UseCallback.js

import { setSelectionRange } from '@testing-library/user-event/dist/utils'
import React, { useCallback, useState } from 'react'

export default function UseCallback({ test, setscore, score, money, setmoney, showScore }) {
  return (
    <div>
      <p>{money},{score}</p>
      <button onClick={() => {
        // setmoney(money + 2)
        setTimeout(() => {
          setscore(score + 1)
          // test()
          showScore()
        }, 1000);
      }}>按</button>
    </div>
  )
}

使用useCallback()的情况,由于点击按钮变化的是score,而添加的依赖是count,所以组件更新是调用的函数始终是缓存的函数,控制台上输出的值也就是缓存的值,不会实时更新

App.js

import logo from './logo.svg';
import { useState, useCallback } from 'react';
import './App.css';
// import UseMemo from './hooks/03-useMemo'
import UseCallback from './hooks/04-useCallback';


function App() {
  const [score, setscore] = useState(0)
  const [money, setmoney] = useState(1)
  const showScore = useCallback(() => {
    console.log(money)
    console.log(score)
  }, [money])

  // const showScore = () => {
  //   console.log(money)
  //   console.log(score)
  // }


  return (
    <div className="App">

      <UseCallback setscore={setscore} score={score} money={money} showScore={showScore} setmoney={setmoney} />
    </div>
  );
}

export default App;

UseCallback.js

import { setSelectionRange } from '@testing-library/user-event/dist/utils'
import React, { useCallback, useState } from 'react'

export default function UseCallback({ setscore, score, money, setmoney, showScore }) {
  return (
    <div>
      <p>{money},{score}</p>
      <button onClick={() => {
        // setmoney(money + 2)
        setTimeout(() => {
          setscore(score + 1)
          // test()
          showScore()
        }, 1000);
      }}>按</button>
    </div>
  )
}

 

posted on 2022-02-16 22:54  mikumikugo  阅读(214)  评论(0编辑  收藏  举报