{"id":1148,"date":"2024-05-16T21:14:55","date_gmt":"2024-05-16T12:14:55","guid":{"rendered":"https:\/\/shinke1987.net\/?p=1148"},"modified":"2024-05-16T21:34:49","modified_gmt":"2024-05-16T12:34:49","slug":"react%ef%bc%9auseref%e3%81%ae%e5%8b%95%e4%bd%9c%e7%a2%ba%e8%aa%8d","status":"publish","type":"post","link":"https:\/\/shinke1987.net\/?p=1148","title":{"rendered":"React\uff1auseRef\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<ul class=\"wp-block-list\">\n<li>\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u304c\u751f\u6210\u3055\u308c\u3066\u304b\u3089\u7834\u68c4\u3055\u308c\u308b\u307e\u3067\u7dad\u6301\u3055\u308c\u308b\u3001\u5909\u66f4\u53ef\u80fd\u306a\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3002<\/li>\n\n\n\n<li>useImperativeHandle\u95a2\u6570\u3092\u5229\u7528\u3059\u308b\u3068\u3001\u89aa\u304b\u3089\u5b50\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/li>\n<\/ul>\n\n\n\n<h2 id=\"toc2\" class=\"wp-block-heading\">\u5b50\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306e\u7279\u5b9a\u30bf\u30b0\u3078\u306e\u53c2\u7167\u3092\u5229\u7528\u3059\u308b<\/h2>\n\n\n\n<h3 id=\"toc3\" class=\"wp-block-heading\">App.tsx\u306e\u5185\u5bb9\uff08\u91cd\u8981\u3067\u306a\u3044\uff09<\/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;;\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<h3 id=\"toc4\" class=\"wp-block-heading\">Parent.tsx\u306e\u5185\u5bb9<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nimport {\n  MutableRefObject, ReactNode, useEffect, useLayoutEffect, useRef, 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  const refStrChildBtn: MutableRefObject&lt;HTMLButtonElement&gt; = useRef(null);\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    refStrChildBtn.current.innerHTML = &#039;aiueo&#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\n        childProp={child1Value}\n        parentStateFunc={setParentValue1}\n        ref={refStrChildBtn}\n      \/&gt;\n    &lt;\/&gt;\n  );\n}\n\nexport default Parent;\n<\/pre><\/div>\n\n\n<h3 id=\"toc5\" 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 React, {\n  forwardRef, LegacyRef, useEffect, useLayoutEffect,\n} from &#039;react&#039;;\n\ntype Child1Props = {\n  childProp: string,\n  parentStateFunc: Function\n};\n\n\/\/ function Child1({ childProp, parentStateFunc }: Child1Props): ReactNode {\nconst Child1: React.ForwardRefExoticComponent&lt;any&gt; = forwardRef(\n  ({ childProp, parentStateFunc }: Child1Props, ref: LegacyRef&lt;HTMLButtonElement&gt;) =&gt; {\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 start Promise date = &#039;, Date());\n    });\n\n    return (\n      &lt;&gt;\n        &lt;div&gt;\n          \u5b50\uff1a\n          {childProp}\n        &lt;\/div&gt;\n        &lt;div&gt;\n          &lt;button\n            type=&quot;button&quot;\n            onClick={childBtnClickFunc}\n            ref={ref}\n          \/&gt;\n        &lt;\/div&gt;\n      &lt;\/&gt;\n    );\n  },\n);\n\/\/ }\n\nexport default Child1;\n<\/pre><\/div>\n\n\n<h3 id=\"toc6\" class=\"wp-block-heading\">\u7d50\u679c<\/h3>\n\n\n\n<p>Parent.tsx \u306e useLayoutEffect \u3067\u30dc\u30bf\u30f3\u306e\u53c2\u7167\u3092\u7d4c\u7531\u3057\u3066<br>innerHTML\u3092\u7de8\u96c6\u3057\u3066\u3044\u308b\u3053\u3068\u306e\u52d5\u4f5c\u78ba\u8a8d\u3092\u3059\u308c\u3070\u826f\u3044\u3002<\/p>\n\n\n\n<p>\uff08\u521d\u671f\u8868\u793a\u5f8c\u306b\u5b50\u306e\u30dc\u30bf\u30f3\u306e\u6587\u5b57\u5217\u304c\u300caiueo\u300d\u3068\u306a\u3063\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3059\u308c\u3070\u826f\u3044\u3002\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u74b0\u5883 React\uff1av18.3.1 Tips \u5b50\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306e\u7279\u5b9a\u30bf\u30b0\u3078\u306e\u53c2\u7167\u3092\u5229\u7528\u3059\u308b App.tsx\u306e\u5185\u5bb9\uff08\u91cd\u8981\u3067\u306a\u3044\uff09 Parent.tsx\u306e\u5185\u5bb9 Child1.tsx\u306e\u5185\u5bb9 \u7d50\u679c Parent.tsx \u306e use [&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-1148","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\/1148","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=1148"}],"version-history":[{"count":2,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1148\/revisions"}],"predecessor-version":[{"id":1158,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1148\/revisions\/1158"}],"wp:attachment":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}