{"id":1150,"date":"2024-05-16T21:20:33","date_gmt":"2024-05-16T12:20:33","guid":{"rendered":"https:\/\/shinke1987.net\/?p=1150"},"modified":"2024-05-16T21:34:17","modified_gmt":"2024-05-16T12:34:17","slug":"react%ef%bc%9ausecontext%e3%81%ae%e5%8b%95%e4%bd%9c%e7%a2%ba%e8%aa%8d","status":"publish","type":"post","link":"https:\/\/shinke1987.net\/?p=1150","title":{"rendered":"React\uff1auseContext\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>\u8907\u6570\u968e\u5c64\u3067\u5024\u3092\u53c2\u7167\u3059\u308b\u6642\u306b\u4fbf\u5229\u306a\u30d5\u30c3\u30af\u3002\uff08Props\u306e\u30d0\u30b1\u30c4\u30ea\u30ec\u30fc\u3057\u306a\u304f\u3066\u3059\u3080\uff09<br>\uff08\u7956\u7236\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3067\u5b9a\u7fa9\u3055\u308c\u305f\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u306e\u5024\u3092\u89aa\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3068\u5b50\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u304b\u3089\u53c2\u7167\u7b49\uff09<\/li>\n<\/ul>\n\n\n\n<h2 id=\"toc2\" class=\"wp-block-heading\">\u8907\u6570\u968e\u5c64\u3067\u5024\u3092\u53c2\u7167\u3059\u308b<\/h2>\n\n\n\n<h3 id=\"toc3\" class=\"wp-block-heading\">App.tsx\u306e\u5185\u5bb9<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nimport React, {\n  createContext, ReactNode, useEffect, useLayoutEffect,\n} from &#039;react&#039;;\nimport Parent from &#039;.\/Parent.tsx&#039;;\n\nexport const TestContext: React.Context&lt;any&gt; = createContext(undefined);\nconst contextValue = {\n  id: &#039;ID1&#039;,\n  name: &#039;aiueo&#039;,\n};\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;TestContext.Provider value={contextValue}&gt;\n      \u3042\u3044\u3046\u3048\u304a\n      &lt;Parent \/&gt;\n    &lt;\/TestContext.Provider&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  ReactNode, useContext, useEffect, useLayoutEffect, useState,\n} from &#039;react&#039;;\nimport Child1 from &#039;.\/Child1.tsx&#039;;\nimport { TestContext } from &#039;.\/App.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 { id, name } = useContext(TestContext);\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    console.log(`Parent ${id} ${name}`);    \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      \/&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 {\n  ReactNode, useContext, useEffect, useLayoutEffect,\n} from &#039;react&#039;;\nimport { TestContext } from &#039;.\/App.tsx&#039;;\n\ntype Child1Props = {\n  childProp: string,\n  parentStateFunc: Function\n};\n\nfunction Child1({ childProp, parentStateFunc }: Child1Props): ReactNode {\n  const { id, name } = useContext(TestContext);\n\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    console.log(`Child1 ${id} ${name}`);\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        &gt;\n          \u5b50\u30dc\u30bf\u30f3\n        &lt;\/button&gt;\n      &lt;\/div&gt;\n    &lt;\/&gt;\n  );\n}\n\nexport default Child1;\n<\/pre><\/div>\n\n\n<h3 id=\"toc6\" class=\"wp-block-heading\">\u7d50\u679c\uff08\u30b3\u30f3\u30bd\u30fc\u30eb\u306e\u8868\u793a\u5185\u5bb9\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nChild1 useLayoutEffect\nChild1 ID1 aiueo\nParent useLayoutEffect\nParent ID1 aiueo\nApp useLayoutEffect\nChild1 useEffect start Promise date =  Thu May 16 2024 12:12:20 GMT+0900 (\u65e5\u672c\u6a19\u6e96\u6642)\nParent useEffect\nApp useEffect\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u74b0\u5883 React\uff1av18.3.1 Tips \u8907\u6570\u968e\u5c64\u3067\u5024\u3092\u53c2\u7167\u3059\u308b App.tsx\u306e\u5185\u5bb9 Parent.tsx\u306e\u5185\u5bb9 Child1.tsx\u306e\u5185\u5bb9 \u7d50\u679c\uff08\u30b3\u30f3\u30bd\u30fc\u30eb\u306e\u8868\u793a\u5185\u5bb9\uff09<\/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-1150","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\/1150","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=1150"}],"version-history":[{"count":2,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1150\/revisions"}],"predecessor-version":[{"id":1157,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1150\/revisions\/1157"}],"wp:attachment":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}