0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ReactのReact.memo、useCallback、useMemoについてまとめてみた

Posted at

ReactのhooksのReact.memo、useCallback、useMemoの3つに
触れる機会があったのでまとめてみました。

  • useCallback(関数をメモ化する)(メモ化→キャッシュに保存する)
const handleClick = useCallback(() => {
+    setCountHeavy(countHeavy + 1);
+  }, [countHeavy]);

countHeavyが変更されない限りhandleClick 関数は実行されない

  • useMemo(値をメモ化する)

countHeavyが変更されない限りwhile文は実行されない

const handleClick = usememo(() => {
+    while(i < 1000000) {
        i++;
     }
+  }, [countHeavy]);

countHeavyが変更されない限りhandleClick 関数は実行されない

  • React.memo(コンポーネントをメモ化する、propsが変更されたら実行)
import React, { useState } from "react";

const Child = React.memo(props => {
console.log("render Child");
return <p>Child: {props.count}</p>;
});

3つともキャッシュする内容が違うだけで概念は同じ。
・useCallback=関数をメモ化する
・useMemo=値をメモ化する
・React.memo=コンポーネントをメモ化する

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?