{"id":1139,"date":"2024-05-16T20:30:35","date_gmt":"2024-05-16T11:30:35","guid":{"rendered":"https:\/\/shinke1987.net\/?p=1139"},"modified":"2024-05-16T21:32:40","modified_gmt":"2024-05-16T12:32:40","slug":"react%ef%bc%9ausestate-%e3%81%ae%e5%8b%95%e4%bd%9c%e7%a2%ba%e8%aa%8d","status":"publish","type":"post","link":"https:\/\/shinke1987.net\/?p=1139","title":{"rendered":"React\uff1auseState \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\">\u5b50\u304b\u3089\u89aa\u306eState\u66f4\u65b0<\/h2>\n\n\n\n<p>\u203b \u89aa\u304b\u3089\u5b50\u306e\u30d1\u30bf\u30fc\u30f3\u306flifting state up \uff08State\u306e\u30ea\u30d5\u30c8\u30a2\u30c3\u30d7\uff09\u3092\u8003\u3048\u305f\u65b9\u304c\u826f\u3044\u3002<\/p>\n\n\n\n<h3 id=\"toc2\" 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 { ReactNode } from &#039;react&#039;;\nimport Parent from &#039;.\/Parent.tsx&#039;;\n\nfunction App(): ReactNode {\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=\"toc3\" 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 { ReactNode, useState } 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;parentBtnClickFunc&#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<h3 id=\"toc4\" 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 } 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  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>","protected":false},"excerpt":{"rendered":"<p>\u74b0\u5883 React\uff1av18.3.1 \u5b50\u304b\u3089\u89aa\u306eState\u66f4\u65b0 \u203b \u89aa\u304b\u3089\u5b50\u306e\u30d1\u30bf\u30fc\u30f3\u306flifting state up \uff08State\u306e\u30ea\u30d5\u30c8\u30a2\u30c3\u30d7\uff09\u3092\u8003\u3048\u305f\u65b9\u304c\u826f\u3044\u3002 App.tsx\u306e\u5185\u5bb9 Parent.tsx\u306e\u5185\u5bb9 Ch [&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-1139","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\/1139","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=1139"}],"version-history":[{"count":2,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1139\/revisions"}],"predecessor-version":[{"id":1154,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1139\/revisions\/1154"}],"wp:attachment":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}