iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 25
1
自我挑戰組

react.js 的學習筆記 (沒在用硬要學系列 第 25

day 25 useMemo 說明 與 組件傳值

  • 分享至 

  • xImage
  •  

useMemo 是一個提高網頁效能的HOOK
它是利用js的緩存技術(Memoization)
它跟useEffect一樣有2個參數

useMemo{()=>{},[]}

而且執行順序useMemo會先被執行


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


const Test1 = (props) => {
  return(
      <div>Test1</div>
  );
}


export default function App(props) {
  const [count, setCount] = useState(0);
  useEffect(()=>{
    console.log("useEffect");
  });
  useMemo(()=>{
    console.log("useMemo");
  });
  return (
    <div>
          <Test1 />
          <p>count:{count}</p>
          <button onClick={(e) => { setCount( count+1 ) }}>ADD</button>
    </div>
  )
}

https://ithelp.ithome.com.tw/upload/images/20200925/20110292GR84lVH58b.png

怎麼使用?


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


const Test1 = (props) => {
  return(
      <div>Test1</div>
  );
}


export default function App(props) {

  const [count, setCount] = useState(0);

  let umemo = useMemo(()=>{
    return count;
  });

  return (
    <div>
          <Test1 />
          <p>count:{count}</p>
          <p>usememo-count:{umemo}</p>
          <button onClick={(e) => { setCount( count+1 ) }}>ADD</button>
    </div>
  )
}

https://ithelp.ithome.com.tw/upload/images/20200925/20110292PlKGNopbVd.png

第二個參數怎麼使用

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


const Test1 = (props) => {
  return(
      <div>Test1</div>
  );
}


export default function App(props) {

  const [count, setCount] = useState(0);
  const [num, setNum] = useState(0);

  let umemo = useMemo(()=>{
    return {count,num};
  },[num]);

  return (
    <div>
          <Test1 />
          <p>count:{count}</p>
          <p>num:{num}</p>
          <p>usememo-count:{umemo.count}</p>
          <p>usememo-num:{umemo.num}</p>
          <button onClick={(e) => { setCount( count+1 );console.log("count"+count); }}>ADD-count</button>
          <button onClick={(e) => { setNum( num+1 );console.log("num"+num); }}>ADD-num</button>
    </div>
  )
}

測試點擊ADD-count 只有count會增加,其他都無反應
https://ithelp.ithome.com.tw/upload/images/20200925/20110292TYeNmarJql.png

測試點擊ADD-num usememo-count會突然變9 跟count一樣數字
https://ithelp.ithome.com.tw/upload/images/20200925/201102926Lec2t3BWA.png

傳值到組件 再用useMemo來控制

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


const Test1 = ({nums,conus}) => {
  let umemo = useMemo(()=>{
    return {nums,conus};
  },[nums]);

  return(
  <div>Test1-num{umemo.nums}-conut{umemo.conus}</div>
  );
}

export default function App() {

  const [count, setCount] = useState(0);
  const [num, setNum] = useState(0);



  return (
    <div>
          <Test1 nums={num} conus={count} />
          <p>count:{count}</p>
          <p>num:{num}</p>
          <button onClick={(e) => { setCount( count+1 );console.log("count"+count); }}>ADD-count</button>
          <button onClick={(e) => { setNum( num+1 );console.log("num"+num); }}>ADD-num</button>
    </div>
  )
}


上一篇
day 24 memo的應用
下一篇
day 26 useCallback 說明與差異
系列文
react.js 的學習筆記 (沒在用硬要學30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言