{"id":13735,"date":"2021-07-22T15:00:26","date_gmt":"2021-07-22T06:00:26","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=13735"},"modified":"2023-09-05T22:04:05","modified_gmt":"2023-09-05T13:04:05","slug":"%e3%80%90react%e3%80%91%e3%80%8cusememo-usecallback%e3%80%8d%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=13735","title":{"rendered":"\u3010React\u3011\u300cuseMemo\/useCallback\u300d\u306b\u3064\u3044\u3066"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u7d50\u8ad6<\/h2>\n\n\n\n<p>\u5225\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306bprops\u3067\u6e21\u3059\u95a2\u6570\u306fuseCallback\u3092\u4f7f\u3044\u3001\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u5185\u3067\u8a08\u7b97\u7d50\u679c\u3092\u5024\u3068\u3057\u3066\u4f7f\u3046\u95a2\u6570\u306b\u5bfe\u3057\u3066\u306fuseMemo\u3092\u4f7f\u3046\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u524d\u63d0<\/h2>\n\n\n\n<p>\u300cuseMemo\u306f\u5024\u3092\u8fd4\u3059\u3002useCallback\u306f\u95a2\u6570\u3092\u8fd4\u3059\u3002\u300d\u3068\u3044\u3046\u524d\u63d0\u3092\u62bc\u3055\u3048\u3066\u304a\u304f\u3053\u3068\u304c\u91cd\u8981\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">useMemo<\/h2>\n\n\n\n<p>\u30e1\u30e2\u5316\u3055\u308c\u305f\u5024\u3092\u8fd4\u3059\u30d5\u30c3\u30af\u3067\u3059\u3002<span style=\"color: #ff0000;\"><strong>\u8a08\u7b97\u7d50\u679c\u3092\u4fdd\u6301<\/strong><\/span>\u3057\u305d\u308c\u3092\u518d\u5229\u7528\u3059\u308b\u624b\u6cd5\u306e\u3053\u3068\u3002\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306e\u518d\u63cf\u753b\u6642\u306e\u5024\u306e\u518d\u8a08\u7b97\u306e\u7121\u99c4\u304c\u306a\u304f\u306a\u308a\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u4e0a\u304c\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3061\u306a\u307f\u306b\u3001useMemo\u306fmemo\u3068\u9055\u3063\u3066\u3042\u3089\u3086\u308b\u5024\u3092\u30e1\u30e2\u5316\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u306a\u306e\u3067\u6975\u7aef\u306a\u8a71jsx\u306a\u3069\u3082\u30e1\u30e2\u5316\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u69cb\u6587<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">useMemo(\u30e1\u30e2\u5316\u3059\u308b\u5024\u3092\u8a08\u7b97\u3059\u308b\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570,\u305d\u306e\u8a08\u7b97\u304c\u4f9d\u5b58\u3059\u308b\u5909\u6570)<\/pre>\n\n\n\n<p>\u7b2c\u4e00\u5f15\u6570\u306f\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u3092\u6307\u5b9a\u3057\u307e\u3059\u304c\u3001\u300c\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u81ea\u4f53\u300d\u3092\u30e1\u30e2\u5316\u3057\u3066\u3044\u308b\u306e\u3067\u306f\u306a\u304f\u300c\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u304c\u8fd4\u3059\u5024\u300d\u3092\u30e1\u30e2\u5316\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u3044\u6240<\/h3>\n\n\n\n<p>\u4f8b\u3048\u3070\u3001\u5024\u3092\u66f4\u65b0\u3059\u308b\u51e6\u7406\u3060\u3051\u3067\u547c\u3093\u3067\u307b\u3057\u3044\u95a2\u6570\u304c\u3042\u3063\u305f\u5834\u5408\u306b\u3001\u5165\u529b\u51e6\u7406\u307e\u3067\u547c\u3070\u308c\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u308b\u5834\u5408\u306a\u3069\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9f\u88c5\u4f8b<\/h3>\n\n\n\n<p>bai\u95a2\u6570\u306f\u95a2\u4fc2\u306e\u306a\u3044Count2\u306e\u5024\u304c\u66f4\u65b0\u3055\u308c\u308b\u5834\u5408\u306f\u547c\u3070\u308c\u307e\u305b\u3093\u3002(useEffect\u306a\u3069\u3067\u66f8\u304f\u3068\u547c\u3070\u308c\u308b\u5ea6\u306b\u63cf\u753b\u3055\u308c\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002\uff09<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import React,{useState,useMemo} from'react';\n\nfunction App(){\n  const [count1,setCount1] = useState(0);\n  const [count2,setCount2] = useState(0);\n  const bai = count =&gt; {\n    return count*2;\n  };\n const baiCount = useMemo(() =&gt; bai(count1), [count1]);\n\n  return(\n    &lt;div&gt;\n      &lt;p&gt;Count1:{count1}&lt;\/p&gt;\n      &lt;p&gt;DubbleCount:{baiCount}&lt;\/p&gt;\n      &lt;button onClick={()=&gt;setCount1(count1+1)}&gt;increment1&lt;\/button&gt;\n      &lt;p&gt;Count2:{count2}&lt;\/p&gt;\n      &lt;button onClick={()=&gt;setCount2(count2+1)}&gt;increment2&lt;\/button&gt;\n    &lt;\/div&gt;\n  );\n}\n\nexport default App;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">useCallback<\/h2>\n\n\n\n<p>\u95a2\u6570\u3092\u5b50\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306b\u6e21\u3057\u305f\u969b\u3001\u89aa\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u304c\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3055\u308c\u308b\u3068\u5b50\u306eprops\u304c\u5225\u7269\u3068\u307f\u306a\u3055\u308c\u3066\u5b50\u3082\u518d\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3055\u308c\u3066\u3057\u307e\u3044\u307e\u3059\u3002\u305d\u3093\u306a\u5834\u5408\u3067\u3082\u30e1\u30e2\u5316\u3057\u305f\u3044\u969b\u306b\u4f7f\u3048\u308b\u30d5\u30c3\u30af\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">React.memo\u3068\u7d44\u307f\u5408\u308f\u305b\u308b\u3002<\/h3>\n\n\n\n<p>\u30e1\u30e2\u5316\u3055\u308c\u305f\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u3092\u8fd4\u3059\u30d5\u30c3\u30af\u3067\u3059\u3002\u751f\u6210\u3057\u305f\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u3092\u4fdd\u6301\u3067\u304d\u308b\u305f\u3081\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306e\u518d\u63cf\u753b\u6642\u306b\u95a2\u6570\u3092\u518d\u5229\u7528\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u30a2\u30ed\u30fc\u95a2\u6570\u3067\u66f8\u3044\u305f\u95a2\u6570\u306f\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u306e\u305f\u3073\u306b\u6bce\u56de\u9055\u3046\u95a2\u6570\u3092\u751f\u6210\u3057\u307e\u3059\u3002\u306a\u306e\u3067\u3001props\u3067\u95a2\u6570\u3092\u6e21\u3057\u305f\u969b\u306b\u6bce\u56de\u5024\u304c\u5909\u308f\u3063\u3066\u3044\u308b\u3068\u5224\u5b9a\u3055\u308c\u3066\u3057\u307e\u3063\u3066\u3001memo\u5316\u3057\u305f\u3068\u3057\u3066\u3082\u518d\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u304c\u884c\u308f\u308c\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>React.memo\u3068\u4f75\u7528\u3059\u308b\u3068\u3001React.memo\u3067\u30e1\u30e2\u5316\u3057\u305f\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306buseCallback\u3067\u30e1\u30e2\u5316\u3057\u305f\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u3092Props\u3068\u3057\u3066\u6e21\u3059\u3053\u3068\u3067\u3001\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306e\u4e0d\u8981\u306a\u518d\u63cf\u753b\u3092\u30b9\u30ad\u30c3\u30d7\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u4e0a\u304c\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30ab\u30b9\u30bf\u30e0\u30d5\u30c3\u30af\u3067\u306f\u9069\u7528\u304c\u5fc5\u9808<\/h3>\n\n\n\n<p>\u306a\u304a\u3001\u30ab\u30b9\u30bf\u30e0\u30d5\u30c3\u30af\u3067\u95a2\u6570\u3092\u5b9a\u7fa9\u3059\u308b\u5834\u5408\u306f\u57fa\u672c\u7684\u306buseCallback\u3092\u9069\u7528\u3059\u308b\u3053\u3068\u304c\u63a8\u5968\u3055\u308c\u307e\u3059\u3002\u306a\u305c\u306a\u3089\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u3055\u307e\u3056\u307e\u306a\u7b87\u6240\u3067\u5229\u7528\u3057\u3001\u3042\u308b\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u304cprops\u3067\u95a2\u6570\u3092\u6e21\u3057\u3066\u306a\u3044\u3068\u3057\u3066\u3082\u5225\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3067\u306f\u6e21\u3059\u53ef\u80fd\u6027\u304c\u3042\u308b\u305f\u3081\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u69cb\u6587<\/h3>\n\n\n\n<p>\u7b2c\u4e00\u5f15\u6570\u306e\u95a2\u6570\u304c\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u306b\u306a\u308a\u30e1\u30e2\u5316\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">useCallback(\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570(\u30e1\u30e2\u5316),[\u4f9d\u5b58\u3057\u3066\u3044\u308bstate])<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u7b2c\u4e8c\u5f15\u6570\uff08\u4f9d\u5b58\u914d\u5217\uff09<\/h4>\n\n\n\n<p>\u7b2c\u4e8c\u5f15\u6570\u304c\u7a7a\u914d\u5217\u306b\u306a\u3063\u3066\u3044\u305f\u5834\u5408\u306f\u521d\u56de\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u306b\u751f\u6210\u3057\u305f\u95a2\u6570\u3092React\u5185\u3067\u4fdd\u6301\u3059\u308b\u3068\u3044\u3046\u610f\u5473\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3082\u3057\u4f9d\u5b58\u3057\u3066\u3044\u308bstate\u304c\u3042\u308b\u5834\u5408\u306f\u6307\u5b9a\u3059\u308b\u3068\u305d\u306estate\u304c\u66f4\u65b0\u3055\u308c\u308b\u305f\u3073\u306b\u95a2\u6570\u304c\u518d\u751f\u6210\u3055\u308c\u307e\u3059\u3002\u6307\u5b9a\u3057\u306a\u3044\u3068\u305a\u3063\u3068\u540c\u3058state\u3092\u4fdd\u6301\u3057\u7d9a\u3051\u3066\u3057\u307e\u3046\u306e\u3067\u6ce8\u610f\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9f\u88c5\u4f8b<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">import React,{useState,useCallback,useEffect,memo} from'react';\n\nconst Button = memo(({id,onClick})=&gt;{\n  useEffect(()=&gt;{console.log(`render${id}`);});\n  return &lt;button onClick={onClick}&gt;{id}&lt;\/button&gt;;\n});\n\nfunction App(){\n  const [count1,setCount1] = useState(0);\n  const increment1 = useCallback(()=&gt; setCount1(count1 + 1),[count1]);\n  \/\/ const increment1 = ()=&gt; setCount1(count1 + 1);\u3000\u2190\u3053\u308c\u3092\u4f7f\u3046\u3068button2\u3092\u30af\u30ea\u30c3\u30af\u3057\u305f\u5834\u5408\u3067\u3082increment1\u306e\u65b9\u306e\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u304c\u518d\u63cf\u753b\u3055\u308c\u308b\u3002\n  const [count2,setCount2]=useState(0);\n  const increment2 = useCallback(()=&gt; setCount2(count=&gt;count+1),[]);\n\n  return(\n    &lt;div&gt;\n      &lt;p&gt;{count1}&lt;\/p&gt;\n      &lt;Button id={'Increment1'} onClick={increment1}\/&gt;&lt;p&gt;{count2}&lt;\/p&gt;\n      &lt;Button id={'Increment2'} onClick={increment2}\/&gt;\n    &lt;\/div&gt;\n  );\n}\n\nexport default App;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">useMemo\u3068useCallback\u306e\u4f7f\u3044\u5206\u3051<\/h2>\n\n\n\n<p>useMemo\u306f\u5358\u7d14\u306b\u8a08\u7b97\u7d50\u679c\u3092\u4fdd\u6301\u3059\u308b\u305f\u3081\u306b\u4f7f\u3044\u307e\u3059\u3002(Vue.js\u306ecomputed\u306b\u8fd1\u3044\u30a4\u30e1\u30fc\u30b8\u3067\u3059\u3002)<\/p>\n\n\n\n<p>useCallback\u306f\u3001React.memo\u3092\u4f7f\u3063\u305f\u3068\u3057\u3066\u3082\u3001\u5373\u6642\u95a2\u6570\u3092props\u3068\u3057\u3066\u6e21\u3059\u3068\u518d\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u304c\u884c\u308f\u308c\u3066\u3057\u307e\u3044\u307e\u3059\u3002\uff08React.memo\u3092\u4f7f\u3063\u305f\u969b\u306e\u30a2\u30f3\u30c1\u30d1\u30bf\u30fc\u30f3\u306e\u3088\u3046\u3067\u3059\u3002\uff09\u5373\u6642\u95a2\u6570\u3092useCallback\u306b\u30e9\u30c3\u30d7\u3059\u308b\u3053\u3068\u3067\u3001\u5225\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306b\u6e21\u3059\u969b\u306b\u518d\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u304c\u8d77\u3053\u3089\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u7528\u9014\u3067\u4f7f\u3048\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3080\u3057\u308d\u3001React.memo\u3068\u4f75\u7528\u3057\u3066\u4f7f\u3046\u4ee5\u5916\u306e\u7528\u9014\u306f\u306a\u3044\u3002memo\u5316\u3055\u308c\u3066\u306a\u3044\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306b\u5bfe\u3057\u3066useCallback\u3067\u30e1\u30e2\u5316\u3057\u305fprops\u3092\u6e21\u3057\u3066\u3082\u7d50\u5c40\u518d\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3055\u308c\u3066\u3057\u307e\u3046\u305f\u3081\u3002<\/p>\n\n\n\n<p>\u307e\u305f\u3001\u30ab\u30b9\u30bf\u30e0\u30d5\u30c3\u30af\u3092\u4f7f\u3063\u305f\u5834\u5408\u306f\u57fa\u672c\u7684\u306b\u306fprops\u3067\u6e21\u3055\u308c\u308b\u3053\u3068\u3082\u60f3\u5b9a\u3057\u3066\u6c4e\u7528\u6027\u3092\u6301\u305f\u305b\u308b\u305f\u3081\u306buseCallback\u3092\u4f7f\u3046\u3053\u3068\u304c\u63a8\u5968\u3055\u308c\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7d50\u8ad6 \u5225\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306bprops\u3067\u6e21\u3059\u95a2\u6570\u306fuseCallback\u3092\u4f7f\u3044\u3001\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u5185\u3067\u8a08\u7b97\u7d50\u679c\u3092\u5024\u3068\u3057\u3066\u4f7f\u3046\u95a2\u6570\u306b\u5bfe\u3057\u3066\u306fuseMemo\u3092\u4f7f\u3046\u3002 \u524d\u63d0 \u300cuseMemo\u306f\u5024\u3092\u8fd4\u3059\u3002useCallback\u306f\u95a2\u6570\u3092\u8fd4 [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[116],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13735"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13735"}],"version-history":[{"count":16,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13735\/revisions"}],"predecessor-version":[{"id":22426,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13735\/revisions\/22426"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13735"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}