{"id":18446,"date":"2022-11-10T23:01:31","date_gmt":"2022-11-10T14:01:31","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=18446"},"modified":"2023-09-07T08:55:18","modified_gmt":"2023-09-06T23:55:18","slug":"%e3%80%90recoil%e3%80%91%e6%a6%82%e8%a6%81","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=18446","title":{"rendered":"\u3010Recoil\u3011\u6982\u8981"},"content":{"rendered":"\n<p>React\u306e\u72b6\u614b\u7ba1\u7406\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u4e00\u3064\u3067\u3059\u30022020\u5e74\u306b\u767a\u8868\u3055\u308c\u3066React\u3092\u4f5c\u3063\u3066\u3044\u308bfacebook\u304c\u958b\u767a\u3057\u3066\u3044\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u305f\u3081\u3053\u308c\u304b\u3089\u4e3b\u6d41\u306b\u306a\u308b\u3068\u3044\u308f\u308c\u3066\u3044\u307e\u3059\u3002\u975e\u5e38\u306b\u7c21\u5358\u306b\u30b0\u30ed\u30fc\u30d0\u30eb\u306a\u72b6\u614b\u7ba1\u7406\u3092\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u30e1\u30ea\u30c3\u30c8<\/h2>\n\n\n\n<ul>\n<li>\u3044\u304f\u3064\u3082\u30b9\u30c8\u30a2\u304c\u5206\u304b\u308c\u3066\u3044\u308b\u306e\u3067\u9593\u9055\u3048\u3066\u72b6\u614b\u3092\u66f4\u65b0\u3057\u3066\u3057\u307e\u3063\u305f\u3068\u3057\u3066\u3082\u5f71\u97ff\u3092\u6700\u5c0f\u9650\u306b\u3068\u3069\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/li>\n\n\n\n<li>useContext\u3068\u9055\u3063\u3066\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u306e\u5bfe\u8c61\u3092\u6700\u5c0f\u9650\u306b\u3068\u3069\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002(Redux\u306a\u3069\u3068\u540c\u3058\u304f\uff09<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ed6\u306e\u72b6\u614b\u7ba1\u7406\u624b\u6cd5\u3068\u306e\u9055\u3044<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Redux\u3068\u306e\u9055\u3044<\/h3>\n\n\n\n<p>Redux\u3067\u306f\u30b9\u30c8\u30a2\u306f\u4e00\u3064\u3067\u3057\u305f\u304c\u3001Recoil\u306f\u30b9\u30c8\u30a2\u3092\u3044\u304f\u3064\u3082\u6301\u3064\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">useContext\u3068\u306e\u9055\u3044<\/h3>\n\n\n\n<p>Recoil\u306a\u3089\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3092\u3042\u308b\u7a0b\u5ea6\u6700\u9069\u5316\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u5177\u4f53\u7684\u306b\u8a00\u3048\u3070\u3001\u300cuseSetRecoilState\u300d\u3068\u3044\u3046\u66f4\u65b0\u95a2\u6570\u3060\u3051\u5b9f\u884c\u3057\u3066\u3044\u308b\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306f\u518d\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3057\u306a\u3044\u3068\u3044\u3046\u8a2d\u5b9a\u306b\u3057\u3066\u304f\u308c\u307e\u3059\u3002\uff08useRecoilState\u3068\u3044\u3046\u53c2\u7167\u95a2\u6570\u3060\u3051\u518d\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3059\u308b\u3002\uff09<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">recoil\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">npm install recoil\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">recoil\u306e\u578b\u5b9a\u7fa9\u30d5\u30a1\u30a4\u30eb\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb(typescript\u306e\u5834\u5408\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">npm i @types\/recoil<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u8a2d\u5b9a<\/h2>\n\n\n\n<p>\u30eb\u30fc\u30c8\u968e\u5c64\u3092<span style=\"color: #ff0000;\"><strong>\u300cRecoilRoot\u300d<\/strong><\/span>\u3067\u56f2\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import { RecoilRoot } from \"recoil\";\n\nfunction App() {\n  return (\n    &lt;RecoilRoot&gt;\n      &lt;div&gt;\n        XXX\n      &lt;\/div&gt;\n    &lt;\/RecoilRoot&gt;\n  );\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u30b9\u30c8\u30a2(atom)\u306e\u4f5c\u6210<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u6587\u5b57\u5217\uff08string)<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">import { atom } from \"recoil\";\n\nexport const xxxState = atom&lt;string&gt;({\n  key: \"xxxState\",\n  default: \"\",\n});<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">default<\/h4>\n\n\n\n<p>\u30b9\u30c8\u30a2\u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5024\u3092\u53d6\u308a\u51fa\u3059\u3002<\/h4>\n\n\n\n<p><span style=\"color: #ff0000;\"><strong>\u300cuseRecoilValue\u300d<\/strong><\/span>\u3068\u3044\u3046\u3082\u306e\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import { xxxState } from \"..\/states\/xxxState\";\nimport { useRecoilValue } from \"recoil\";\n\nconst xxx = useRecoilValue(xxxState);<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u5024\u3092\u8a2d\u5b9a\u3059\u308b<\/h4>\n\n\n\n<p><span style=\"color: #ff0000;\"><strong>\u300cuseSetRecoilState\u300d<\/strong><\/span>\u3068\u3044\u3046\u3082\u306e\u3092\u4f7f\u3044\u307e\u3059\u3002\u306a\u304a\u3001\u5024\u3092\u8a2d\u5b9a\u3059\u308b\u3060\u3051\u306e\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306f\u518d\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u306f\u767a\u751f\u3057\u307e\u305b\u3093\u3002\uff08\u518d\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u306e\u6700\u9069\u5316\uff09<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import { xxxState } from \"..\/states\/xxxState\";\nimport { useSetRecoilState } from \"recoil\";\n\nconst setXXX = useSetRecoilState(xxxState);\nsetXXX(\"\u8a2d\u5b9a\u5024\");\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u914d\u5217<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">export const xxxState = atom&lt;string[]&gt;({\n  key: \"xxxState\",\n  default: [],\n});<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u5024\u3092\u8a2d\u5b9a\u3059\u308b<\/h4>\n\n\n\n<p>\u914d\u5217\u306e\u5834\u5408\u306f\u5143\u306e\u914d\u5217\u3082\u4e00\u7dd2\u306b\u6307\u5b9a\u3057\u3066\u3042\u3052\u308b\u5fc5\u8981\u304c\u3042\u308b\u306e\u3067\u3001\u300c<span style=\"color: #ff0000;\"><strong>useRecoilValue\u300d\u3068\u30bb\u30c3\u30c8\u3067\u4f7f\u3046\u306e\u304c\u4e00\u822c\u7684\u306b\u306a\u308a\u307e\u3059\u3002<\/strong><\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import { xxxState } from \"..\/states\/xxxState\";\nimport { useSetRecoilState } from \"recoil\";\n\nconst setXXX = useSetRecoilState(xxxState);\nsetXXX([...\u5143\u306e\u914d\u5217\u5024,\u8ffd\u52a0\u3059\u308b\u5024);\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u30b9\u30c8\u30a2(atom)\u306b\u5bfe\u3059\u308b\u547d\u4ee4<\/h2>\n\n\n\n<p><span style=\"color: #ff0000;\"><strong>selector<\/strong><\/span>\u3068\u3044\u3046\u3082\u306e\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import { selector } from \"recoil\";\n\nexport const \u51e6\u7406\u540d = selector&lt;number&gt;({\n  key: \"\u51e6\u7406\u540d\",\n  get: ({ get }) =&gt; {\n    const \u5909\u6570 = get(xxxState);\n    return \u51e6\u7406;\n  },\n});\u3086<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">get<\/h3>\n\n\n\n<p>atom\u540d\u3092\u5f15\u6570\u306b\u6e21\u305b\u3070\u3001\u4ed6\u306eatom\u306b\u30a2\u30af\u30bb\u30b9\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30e6\u30fc\u30b9\u30b1\u30fc\u30b9<\/h3>\n\n\n\n<ul>\n<li>\u914d\u5217\u306e\u9577\u3055\u3092\u53d6\u5f97\u3057\u305f\u3044<\/li>\n\n\n\n<li>\u975e\u540c\u671f\u51e6\u7406\u3067\u4f55\u3089\u304b\u306e\u51e6\u7406\u3092\u52a0\u3048\u305f\u3044\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"React\u306e\u72b6\u614b\u7ba1\u7406\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u4e00\u3064\u3067\u3059\u30022020\u5e74\u306b\u767a\u8868\u3055\u308c\u3066React\u3092\u4f5c\u3063\u3066\u3044\u308bfacebook\u304c\u958b\u767a\u3057\u3066\u3044\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u305f\u3081\u3053\u308c\u304b\u3089\u4e3b\u6d41\u306b\u306a\u308b\u3068\u3044\u308f\u308c\u3066\u3044\u307e\u3059\u3002\u975e\u5e38\u306b\u7c21\u5358\u306b\u30b0\u30ed\u30fc\u30d0\u30eb\u306a\u72b6\u614b\u7ba1\u7406\u3092\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002 \u30e1 [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[116],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/18446"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18446"}],"version-history":[{"count":14,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/18446\/revisions"}],"predecessor-version":[{"id":22451,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/18446\/revisions\/22451"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18446"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}