{"id":1141,"date":"2024-05-16T20:49:25","date_gmt":"2024-05-16T11:49:25","guid":{"rendered":"https:\/\/shinke1987.net\/?p=1141"},"modified":"2025-07-05T23:10:10","modified_gmt":"2025-07-05T14:10:10","slug":"react%ef%bc%9auselayouteffect-%e3%81%a8-useeffect-%e3%81%ae%e5%8b%95%e4%bd%9c%e7%a2%ba%e8%aa%8d","status":"publish","type":"post","link":"https:\/\/shinke1987.net\/?p=1141","title":{"rendered":"React\uff1auseLayoutEffect \u3068 useEffect \u306e\u52d5\u4f5c\u78ba\u8a8d"},"content":{"rendered":"\n<h2 id=\"toc0\" class=\"wp-block-heading\">\u74b0\u5883<\/h2>\n\n\n\n<p>React\uff1av18.3.1<\/p>\n\n\n\n<h2 id=\"toc1\" class=\"wp-block-heading\">Tips<\/h2>\n\n\n\n<p>\u958b\u767a\u74b0\u5883\u3067 useLayoutEffect \u3068 useEffect \u304c2\u56de\u5b9f\u884c\u3055\u308c\u308b\u306e\u3092\u9632\u3050\u306b\u306f\u3001<br>main.tsx \u306e &lt;React.StrictMode&gt; \u306e\u30bf\u30b0\u3092\u30b3\u30e1\u30f3\u30c8\u30a2\u30a6\u30c8\u3059\u308c\u3070\u826f\u3044\u3002<\/p>\n\n\n\n<h2 id=\"toc2\" class=\"wp-block-heading\">\u5b9f\u884c\u3055\u308c\u308b\u9806\u756a\u3092\u78ba\u8a8d<\/h2>\n\n\n\n<h3 id=\"toc3\" class=\"wp-block-heading\">\u518d\u63cf\u753b\u6642\u306b\u5e38\u306b\u5b9f\u884c\u3055\u308c\u308b\u5834\u5408<\/h3>\n\n\n\n<h4 id=\"toc4\" class=\"wp-block-heading\">App.tsx\u306e\u5185\u5bb9<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nimport { ReactNode, useEffect, useLayoutEffect } from &#039;react&#039;;\nimport Parent from &#039;.\/Parent.tsx&#039;;\n\nfunction App(): ReactNode {\n  useLayoutEffect(() =&gt; {\n    console.log(&#039;App useLayoutEffect&#039;);\n  });\n\n  useEffect(() =&gt; {\n    console.log(&#039;App useEffect&#039;);\n  });\n\n  return (\n    &lt;&gt;\n      \u3042\u3044\u3046\u3048\u304a\n      &lt;Parent \/&gt;\n    &lt;\/&gt;\n  );\n}\n\nexport default App;\n<\/pre><\/div>\n\n\n<h4 id=\"toc5\" class=\"wp-block-heading\">Parent.tsx\u306e\u5185\u5bb9<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nimport {\n  ReactNode, useEffect, useLayoutEffect, useState,\n} from &#039;react&#039;;\nimport Child1 from &#039;.\/Child1.tsx&#039;;\n\nfunction Parent(): ReactNode {\n  const &#x5B;child1Value, setChild1Value] = useState&lt;string&gt;(&#039;Child1\u306e\u521d\u671f\u5024&#039;);\n  const &#x5B;parentValue1, setParentValue1] = useState&lt;string&gt;(&#039;Parent\u306e\u521d\u671f\u5024&#039;);\n\n  function parentBtnClickFunc() {\n    alert(&#039;parentBtnClickFunc&#039;);\n    setChild1Value(&#039;\u89aa\u304b\u3089State\u304c\u5909\u66f4\u3055\u308c\u307e\u3057\u305f&#039;);\n  }\n\n  useLayoutEffect(() =&gt; {\n    console.log(&#039;Parent useLayoutEffect&#039;);\n  });\n\n  useEffect(() =&gt; {\n    console.log(&#039;Parent useEffect&#039;);\n  });\n\n  return (\n    &lt;&gt;\n      &lt;div&gt;\n        \u89aa\uff1a\n        {parentValue1}\n      &lt;\/div&gt;\n      &lt;button type=&quot;button&quot; onClick={parentBtnClickFunc}&gt;\n        \u89aa\u30dc\u30bf\u30f3\n      &lt;\/button&gt;\n      &lt;Child1 childProp={child1Value} parentStateFunc={setParentValue1} \/&gt;\n    &lt;\/&gt;\n  );\n}\n\nexport default Parent;\n<\/pre><\/div>\n\n\n<h4 id=\"toc6\" class=\"wp-block-heading\">Child1.tsx\u306e\u5185\u5bb9<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nimport { ReactNode, useEffect, useLayoutEffect } from &#039;react&#039;;\n\ntype Child1Props = {\n  childProp: string,\n  parentStateFunc: Function\n};\n\nfunction Child1({ childProp, parentStateFunc }: Child1Props): ReactNode {\n  function childBtnClickFunc(): void {\n    alert(&#039;childBtnClickFunc&#039;);\n    parentStateFunc(&#039;\u5b50\u304b\u3089State\u304c\u5909\u66f4\u3055\u308c\u307e\u3057\u305f&#039;);\n  }\n\n  useLayoutEffect(() =&gt; {\n    console.log(&#039;Child1 useLayoutEffect&#039;);\n  });\n\n  useEffect(() =&gt; {\n    console.log(&#039;Child1 useEffect&#039;);\n  });\n\n  return (\n    &lt;&gt;\n      &lt;div&gt;\n        \u5b50\uff1a\n        {childProp}\n      &lt;\/div&gt;\n      &lt;button type=&quot;button&quot; onClick={childBtnClickFunc}&gt;\n        \u5b50\u30dc\u30bf\u30f3\n      &lt;\/button&gt;\n    &lt;\/&gt;\n  );\n}\n\nexport default Child1;\n<\/pre><\/div>\n\n\n<h4 id=\"toc7\" class=\"wp-block-heading\">\u7d50\u679c\uff08\u30b3\u30f3\u30bd\u30fc\u30eb\u306e\u8868\u793a\u5185\u5bb9\uff09<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ \u30da\u30fc\u30b8\u8868\u793a\u5f8c\u3002\nChild1 useLayoutEffect\nParent useLayoutEffect\nApp useLayoutEffect\nChild1 useEffect\nParent useEffect\nApp useEffect\n\n\/\/ \u89aa\u30dc\u30bf\u30f3\u3092\u62bc\u4e0b\u3057\u3001\u5b50\u306eProp\uff08\u89aa\u306eState\uff09\u3092\u66f4\u65b0\u5f8c\u3002\nChild1 useLayoutEffect\nParent useLayoutEffect\nChild1 useEffect\nParent useEffect\n\n\/\/ \u5b50\u30dc\u30bf\u30f3\u3092\u62bc\u4e0b\u3057\u3001\u89aa\u306eState\u3092\u66f4\u65b0\u5f8c\u3002\nChild1 useLayoutEffect\nParent useLayoutEffect\nChild1 useEffect\nParent useEffect\n<\/pre><\/div>\n\n\n<h3 id=\"toc8\" class=\"wp-block-heading\">\u521d\u671f\u63cf\u753b\u6642\u306b\u306e\u307f\u5b9f\u884c\u3055\u308c\u308b\u5834\u5408<\/h3>\n\n\n\n<h4 id=\"toc9\" class=\"wp-block-heading\">App.tsx\u306e\u5185\u5bb9<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nimport { ReactNode, useEffect, useLayoutEffect } from &#039;react&#039;;\nimport Parent from &#039;.\/Parent.tsx&#039;;\n\nfunction App(): ReactNode {\n  useLayoutEffect(() =&gt; {\n    console.log(&#039;App useLayoutEffect&#039;);\n  }, &#x5B;]);\n\n  useEffect(() =&gt; {\n    console.log(&#039;App useEffect&#039;);\n  }, &#x5B;]);\n\n  return (\n    &lt;&gt;\n      \u3042\u3044\u3046\u3048\u304a\n      &lt;Parent \/&gt;\n    &lt;\/&gt;\n  );\n}\n\nexport default App;\n<\/pre><\/div>\n\n\n<h4 id=\"toc10\" class=\"wp-block-heading\">Parent.tsx\u306e\u5185\u5bb9<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nimport {\n  ReactNode, useEffect, useLayoutEffect, useState,\n} from &#039;react&#039;;\nimport Child1 from &#039;.\/Child1.tsx&#039;;\n\nfunction Parent(): ReactNode {\n  const &#x5B;child1Value, setChild1Value] = useState&lt;string&gt;(&#039;Child1\u306e\u521d\u671f\u5024&#039;);\n  const &#x5B;parentValue1, setParentValue1] = useState&lt;string&gt;(&#039;Parent\u306e\u521d\u671f\u5024&#039;);\n\n  function parentBtnClickFunc() {\n    alert(&#039;parentBtnClickFunc&#039;);\n    setChild1Value(&#039;\u89aa\u304b\u3089State\u304c\u5909\u66f4\u3055\u308c\u307e\u3057\u305f&#039;);\n  }\n\n  useLayoutEffect(() =&gt; {\n    console.log(&#039;Parent useLayoutEffect&#039;);\n  }, &#x5B;]);\n\n  useEffect(() =&gt; {\n    console.log(&#039;Parent useEffect&#039;);\n  }, &#x5B;]);\n\n  return (\n    &lt;&gt;\n      &lt;div&gt;\n        \u89aa\uff1a\n        {parentValue1}\n      &lt;\/div&gt;\n      &lt;button type=&quot;button&quot; onClick={parentBtnClickFunc}&gt;\n        \u89aa\u30dc\u30bf\u30f3\n      &lt;\/button&gt;\n      &lt;Child1 childProp={child1Value} parentStateFunc={setParentValue1} \/&gt;\n    &lt;\/&gt;\n  );\n}\n\nexport default Parent;\n<\/pre><\/div>\n\n\n<h4 id=\"toc11\" class=\"wp-block-heading\">Child1.tsx\u306e\u5185\u5bb9<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nimport { ReactNode, useEffect, useLayoutEffect } from &#039;react&#039;;\n\ntype Child1Props = {\n  childProp: string,\n  parentStateFunc: Function\n};\n\nfunction Child1({ childProp, parentStateFunc }: Child1Props): ReactNode {\n  function childBtnClickFunc(): void {\n    alert(&#039;childBtnClickFunc&#039;);\n    parentStateFunc(&#039;\u5b50\u304b\u3089State\u304c\u5909\u66f4\u3055\u308c\u307e\u3057\u305f&#039;);\n  }\n\n  useLayoutEffect(() =&gt; {\n    console.log(&#039;Child1 useLayoutEffect&#039;);\n  }, &#x5B;]);\n\n  useEffect(() =&gt; {\n    console.log(&#039;Child1 useEffect&#039;);\n  }, &#x5B;]);\n\n  return (\n    &lt;&gt;\n      &lt;div&gt;\n        \u5b50\uff1a\n        {childProp}\n      &lt;\/div&gt;\n      &lt;button type=&quot;button&quot; onClick={childBtnClickFunc}&gt;\n        \u5b50\u30dc\u30bf\u30f3\n      &lt;\/button&gt;\n    &lt;\/&gt;\n  );\n}\n\nexport default Child1;\n<\/pre><\/div>\n\n\n<h4 id=\"toc12\" class=\"wp-block-heading\">\u7d50\u679c\uff08\u30b3\u30f3\u30bd\u30fc\u30eb\u306e\u8868\u793a\u5185\u5bb9\uff09<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ \u30da\u30fc\u30b8\u8868\u793a\u5f8c\u3002\nChild1 useLayoutEffect\nParent useLayoutEffect\nApp useLayoutEffect\nChild1 useEffect\nParent useEffect\nApp useEffect\n\n\/\/ \u89aa\u30dc\u30bf\u30f3\u3092\u62bc\u4e0b\u3057\u3001\u5b50\u306eProp\uff08\u89aa\u306eState\uff09\u3092\u66f4\u65b0\u5f8c\u3002\n\u203b \u8ffd\u52a0\u3067\u8868\u793a\u3055\u308c\u306a\u3044\u3002\n\n\/\/ \u5b50\u30dc\u30bf\u30f3\u3092\u62bc\u4e0b\u3057\u3001\u89aa\u306eState\u3092\u66f4\u65b0\u5f8c\u3002\n\u203b \u8ffd\u52a0\u3067\u8868\u793a\u3055\u308c\u306a\u3044\u3002\n<\/pre><\/div>\n\n\n<h3 id=\"toc13\" class=\"wp-block-heading\">\u7279\u5b9a\u306eState\u304c\u66f4\u65b0\u3055\u308c\u305f\u6642\u306b\u5b9f\u884c\u3055\u308c\u308b\u5834\u5408<\/h3>\n\n\n\n<h4 id=\"toc14\" class=\"wp-block-heading\">App.tsx\u306e\u5185\u5bb9<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nimport { ReactNode, useEffect, useLayoutEffect } from &#039;react&#039;;\nimport Parent from &#039;.\/Parent.tsx&#039;;\n\nfunction App(): ReactNode {\n  useLayoutEffect(() =&gt; {\n    console.log(&#039;App useLayoutEffect&#039;);\n  }, &#x5B;]);\n\n  useEffect(() =&gt; {\n    console.log(&#039;App useEffect&#039;);\n  }, &#x5B;]);\n\n  return (\n    &lt;&gt;\n      \u3042\u3044\u3046\u3048\u304a\n      &lt;Parent \/&gt;\n    &lt;\/&gt;\n  );\n}\n\nexport default App;\n<\/pre><\/div>\n\n\n<h4 id=\"toc15\" class=\"wp-block-heading\">Parent.tsx\u306e\u5185\u5bb9<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nimport {\n  ReactNode, useEffect, useLayoutEffect, useState,\n} from &#039;react&#039;;\nimport Child1 from &#039;.\/Child1.tsx&#039;;\n\nfunction Parent(): ReactNode {\n  const &#x5B;child1Value, setChild1Value] = useState&lt;string&gt;(&#039;Child1\u306e\u521d\u671f\u5024&#039;);\n  const &#x5B;parentValue1, setParentValue1] = useState&lt;string&gt;(&#039;Parent\u306e\u521d\u671f\u5024&#039;);\n\n  function parentBtnClickFunc() {\n    alert(&#039;parentBtnClickFunc&#039;);\n    setChild1Value(&#039;\u89aa\u304b\u3089State\u304c\u5909\u66f4\u3055\u308c\u307e\u3057\u305f&#039;);\n  }\n\n  useLayoutEffect(() =&gt; {\n    console.log(&#039;Parent useLayoutEffect&#039;);\n  }, &#x5B;parentValue1]);\n\n  useEffect(() =&gt; {\n    console.log(&#039;Parent useEffect&#039;);\n  }, &#x5B;parentValue1]);\n\n  return (\n    &lt;&gt;\n      &lt;div&gt;\n        \u89aa\uff1a\n        {parentValue1}\n      &lt;\/div&gt;\n      &lt;button type=&quot;button&quot; onClick={parentBtnClickFunc}&gt;\n        \u89aa\u30dc\u30bf\u30f3\n      &lt;\/button&gt;\n      &lt;Child1 childProp={child1Value} parentStateFunc={setParentValue1} \/&gt;\n    &lt;\/&gt;\n  );\n}\n\nexport default Parent;\n<\/pre><\/div>\n\n\n<h4 id=\"toc16\" class=\"wp-block-heading\">Child1.tsx\u306e\u5185\u5bb9<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nimport { ReactNode, useEffect, useLayoutEffect } from &#039;react&#039;;\n\ntype Child1Props = {\n  childProp: string,\n  parentStateFunc: Function\n};\n\nfunction Child1({ childProp, parentStateFunc }: Child1Props): ReactNode {\n  function childBtnClickFunc(): void {\n    alert(&#039;childBtnClickFunc&#039;);\n    parentStateFunc(&#039;\u5b50\u304b\u3089State\u304c\u5909\u66f4\u3055\u308c\u307e\u3057\u305f&#039;);\n  }\n\n  useLayoutEffect(() =&gt; {\n    console.log(&#039;Child1 useLayoutEffect&#039;);\n  }, &#x5B;childProp]);\n\n  useEffect(() =&gt; {\n    console.log(&#039;Child1 useEffect&#039;);\n  }, &#x5B;childProp]);\n\n  return (\n    &lt;&gt;\n      &lt;div&gt;\n        \u5b50\uff1a\n        {childProp}\n      &lt;\/div&gt;\n      &lt;button type=&quot;button&quot; onClick={childBtnClickFunc}&gt;\n        \u5b50\u30dc\u30bf\u30f3\n      &lt;\/button&gt;\n    &lt;\/&gt;\n  );\n}\n\nexport default Child1;\n<\/pre><\/div>\n\n\n<h4 id=\"toc17\" class=\"wp-block-heading\">\u7d50\u679c\uff08\u30b3\u30f3\u30bd\u30fc\u30eb\u306e\u8868\u793a\u5185\u5bb9\uff09<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ \u30da\u30fc\u30b8\u8868\u793a\u5f8c\u3002\nChild1 useLayoutEffect\nParent useLayoutEffect\nApp useLayoutEffect\nChild1 useEffect\nParent useEffect\nApp useEffect\n\n\/\/ \u89aa\u30dc\u30bf\u30f3\u3092\u62bc\u4e0b\u3057\u3001\u5b50\u306eProp\uff08\u89aa\u306eState\uff09\u3092\u66f4\u65b0\u5f8c\u3002\nChild1 useLayoutEffect\nChild1 useEffect\n\n\/\/ \u5b50\u30dc\u30bf\u30f3\u3092\u62bc\u4e0b\u3057\u3001\u89aa\u306eState\u3092\u66f4\u65b0\u5f8c\u3002\nParent useLayoutEffect\nParent useEffect\n<\/pre><\/div>\n\n\n<h2 id=\"toc18\" class=\"wp-block-heading\">useEffect \u3067 fetch \u3057\u3066State\u66f4\u65b0<\/h2>\n\n\n\n<h3 id=\"toc19\" class=\"wp-block-heading\">Child1.tsx\u306e\u5185\u5bb9<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nimport { ReactNode, useEffect, useLayoutEffect } from &#039;react&#039;;\n\ntype Child1Props = {\n  childProp: string,\n  parentStateFunc: Function\n};\n\nfunction Child1({ childProp, parentStateFunc }: Child1Props): ReactNode {\n  const &#x5B;showText, setShowText] = useState(&#039;\u521d\u671f\u5024&#039;);\n\n  const fetchData = async () =&gt; {\n    const response = await fetch(&#039;http:\/\/localhost:3001&#039;);\n    const data = await response.text();\n    return data;\n  };\n\n  useEffect(() =&gt; {\n    fetchData().then((text:string) =&gt; {\n      setShowText(text);\n    });\n  }, &#x5B;]);\n\n  return (\n    &lt;&gt;\n      &lt;div&gt;\n        \u5b50\uff1a\n        {childProp}\n      &lt;\/div&gt;\n      &lt;div&gt;\n        {showText}\n      &lt;\/div&gt;\n    &lt;\/&gt;\n  );\n}\n\nexport default Child1;\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u74b0\u5883 React\uff1av18.3.1 Tips \u958b\u767a\u74b0\u5883\u3067 useLayoutEffect \u3068 useEffect \u304c2\u56de\u5b9f\u884c\u3055\u308c\u308b\u306e\u3092\u9632\u3050\u306b\u306f\u3001main.tsx \u306e &lt;React.StrictMode&gt; \u306e\u30bf\u30b0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[104],"tags":[105],"class_list":["post-1141","post","type-post","status-publish","format-standard","hentry","category-react","tag-react"],"_links":{"self":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1141"}],"version-history":[{"count":5,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1141\/revisions"}],"predecessor-version":[{"id":1324,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1141\/revisions\/1324"}],"wp:attachment":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}