{"id":2731,"date":"2023-08-02T09:35:18","date_gmt":"2023-08-02T00:35:18","guid":{"rendered":"https:\/\/www.agile-software.site\/?p=2731"},"modified":"2023-08-02T10:26:04","modified_gmt":"2023-08-02T01:26:04","slug":"usestate%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"https:\/\/agile-software.net\/?p=2731","title":{"rendered":"useState\u306b\u3064\u3044\u3066"},"content":{"rendered":"\n<p>React\u306e <code>useState<\/code> \u30d5\u30c3\u30af\u306f\u3001\u95a2\u6570\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3067\u72b6\u614b\u3092\u7ba1\u7406\u3059\u308b\u305f\u3081\u306e\u3082\u306e\u3067\u3059\u3002<code>useState<\/code>\u306b\u5f15\u6570\u3068\u3057\u3066\u6e21\u3055\u308c\u308b\u5024\u306f\u3001\u305d\u306e\u72b6\u614b\u306e\u521d\u671f\u5024\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p><code>useState(null)<\/code>\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u72b6\u614b\u5909\u6570\u306e\u521d\u671f\u5024\u304c<code>null<\/code>\u306b\u8a2d\u5b9a\u3055\u308c\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u72b6\u614b\u304c\u521d\u671f\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u6642\u306b\u672a\u5b9a\u7fa9\u307e\u305f\u306f\u7a7a\u3067\u3042\u308b\u3079\u304d\u5834\u5408\u3001\u307e\u305f\u306f\u7279\u5b9a\u306e\u5024\u304c\u307e\u3060\u5229\u7528\u53ef\u80fd\u3067\u306a\u3044\u5834\u5408\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u306b\u4e00\u4f8b\u3092\u6319\u3052\u307e\u3059\uff1a<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>import React, { useState } from &#39;react&#39;;\n\nfunction MyComponent() {\n  \/\/ user\u72b6\u614b\u306e\u521d\u671f\u5024\u3092null\u306b\u8a2d\u5b9a\n  const [user, setUser] = useState(null);\n\n  \/\/ \u4f55\u3089\u304b\u306e\u975e\u540c\u671f\u64cd\u4f5c\uff08API\u547c\u3073\u51fa\u3057\u3001\u30c7\u30fc\u30bf\u53d6\u5f97\u7b49\uff09\u3092\u884c\u3044\u3001\n  \/\/ \u7d50\u679c\u304c\u5f97\u3089\u308c\u6b21\u7b2c\u3001setUser\u3092\u547c\u3073\u51fa\u3057\u3066user\u72b6\u614b\u3092\u66f4\u65b0\u3059\u308b\u3068\u60f3\u5b9a\n  \/\/ \u305d\u306e\u7d50\u679c\u304c\u5f97\u3089\u308c\u308b\u307e\u3067\u3001user\u306fnull\u306e\u307e\u307e\u3067\u3059\u3002\n\n  if (user === null) {\n    return &lt;div&gt;Loading...&lt;\/div&gt;; \/\/ \u30c7\u30fc\u30bf\u304c\u307e\u3060\u5f97\u3089\u308c\u3066\u3044\u306a\u3044\u5834\u5408\u306e\u51e6\u7406\n  } else {\n    return &lt;div&gt;Welcome, {user.name}!&lt;\/div&gt;; \/\/ \u30c7\u30fc\u30bf\u304c\u5f97\u3089\u308c\u305f\u5834\u5408\u306e\u51e6\u7406\n  }\n}\n\nexport default MyComponent;<\/code><\/pre><\/div>\n\n\n\n<p>\u3053\u306e\u3088\u3046\u306b\u3001<code>useState(null)<\/code>\u306f\u3001\u72b6\u614b\u306e\u521d\u671f\u5024\u304c\u660e\u793a\u7684\u306b<code>null<\/code>\u3067\u3042\u308b\u3079\u304d\u5834\u5408\u3001\u307e\u305f\u306f\u975e\u540c\u671f\u306b\u53d6\u5f97\u3059\u308b\u30c7\u30fc\u30bf\u3092\u5f85\u3064\u5834\u5408\u306a\u3069\u306b\u4fbf\u5229\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>React\u306e useState \u30d5\u30c3\u30af\u306f\u3001\u95a2\u6570\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3067\u72b6\u614b\u3092\u7ba1\u7406\u3059\u308b\u305f\u3081\u306e\u3082\u306e\u3067\u3059\u3002useState\u306b\u5f15\u6570\u3068\u3057\u3066\u6e21\u3055\u308c\u308b\u5024\u306f\u3001\u305d\u306e\u72b6\u614b\u306e\u521d\u671f\u5024\u306b\u306a\u308a\u307e\u3059\u3002 useState(null)\u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u72b6\u614b\u5909\u6570\u306e\u521d\u671f<\/p>\n","protected":false},"author":1,"featured_media":2740,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jin_ogp_image_url":"","_jin_last_featured_id":0,"footnotes":""},"categories":[21],"tags":[],"class_list":["post-2731","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react"],"_links":{"self":[{"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts\/2731","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2731"}],"version-history":[{"count":2,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts\/2731\/revisions"}],"predecessor-version":[{"id":2747,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts\/2731\/revisions\/2747"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/media\/2740"}],"wp:attachment":[{"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}