{"id":4181,"date":"2023-03-27T10:29:25","date_gmt":"2023-03-27T01:29:25","guid":{"rendered":"https:\/\/weseek.co.jp\/tech\/?p=4181"},"modified":"2023-04-17T10:32:01","modified_gmt":"2023-04-17T01:32:01","slug":"swr-2-0-%e3%81%a7%e8%bf%bd%e5%8a%a0%e3%81%95%e3%82%8c%e3%81%9f-useswrmutation-%e3%81%ae%e4%bd%bf%e3%81%84%e6%89%80","status":"publish","type":"post","link":"https:\/\/weseek.co.jp\/tech\/4181\/","title":{"rendered":"SWR 2.0 \u3067\u8ffd\u52a0\u3055\u308c\u305f useSWRMutation \u306e\u4f7f\u3044\u6240"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3002<a href=\"https:\/\/github.com\/weseek\/growi\/\">GROWI<\/a> \u30a8\u30f3\u30b8\u30cb\u30a2\u306e <a href=\"https:\/\/github.com\/miya\">\u5bae\u6ca2<\/a> \u3067\u3059\u3002\u4eca\u56de\u306f\u4fbf\u5229\u306a React hooks \u3067\u3042\u308b <a href=\"https:\/\/github.com\/vercel\/swr\">SWR<\/a> 2.0 \u3067\u8ffd\u52a0\u3055\u308c\u305f useSWRMutation \u306e\u4f7f\u3044\u6240\u306b\u3064\u3044\u3066\u7c21\u5358\u306b\u7d39\u4ecb\u3057\u3088\u3046\u3068\u601d\u3044\u307e\u3059\u3002\u3042\u308b\u7a0b\u5ea6 SWR \u3092\u4f7f\u3063\u305f\u3053\u3068\u304c\u3042\u308b\u4eba\u5411\u3051\u306e\u8a18\u4e8b\u3068\u306a\u3063\u3066\u304a\u308a\u307e\u3059\u3002<\/p>\n<h2>\u4f7f\u3044\u6240<\/h2>\n<p>\u300cuseSWRMutation\u300d\u3068\u306f\u3001\u624b\u52d5\u3067\u30c7\u30fc\u30bf\u306e\u30df\u30e5\u30fc\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u308b React Hooks \u306e1\u3064\u3067\u3042\u308a\u3001API \u306e POST\u3001PUT\u3001DELETE \u306a\u3069\u306e HTTP \u30ea\u30af\u30a8\u30b9\u30c8\u3092\u30c8\u30ea\u30ac\u30fc\u306b\u3057\u3066\u3001\u30c7\u30fc\u30bf\u306e\u66f4\u65b0\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br \/>\n\u3053\u308c\u306b\u3088\u308a\u3001\u30e6\u30fc\u30b6\u30fc\u306f\u4efb\u610f\u306e\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u30c7\u30fc\u30bf\u306e\u53d6\u5f97\u304c\u53ef\u80fd\u306b\u306a\u308a\u3001\u9045\u5ef6\u8aad\u307f\u8fbc\u307f\u306a\u3069\u306e UX \u306e\u5411\u4e0a\u306b\u3082\u7e4b\u304c\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u30011\u3064\u306e\u30da\u30fc\u30b8\u306b\u591a\u6570\u306e\u753b\u50cf\u304c\u3042\u308b\u5834\u5408\u30011\u56de\u306e\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3067\u5168\u3066\u306e\u753b\u50cf\u3092\u8aad\u307f\u8fbc\u3093\u3067\u3057\u307e\u3046\u3068\u8aad\u307f\u8fbc\u307f\u306b\u6642\u9593\u304c\u639b\u304b\u3063\u3066\u3057\u307e\u3046\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u3053\u3067\u3001\u521d\u56de\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u6642\u306b\u30e6\u30fc\u30b6\u30fc\u304c\u8996\u8a8d\u53ef\u80fd\u306a\u90e8\u5206\u307e\u3067\u753b\u50cf\u306e\u8aad\u307f\u8fbc\u307f\u3092\u3057\u3066\u3001\u30b9\u30af\u30ed\u30fc\u30eb\u3057\u305f\u30bf\u30a4\u30df\u30f3\u30b0\u306a\u3069\u3067\u5fc5\u8981\u306a\u90e8\u5206\u306e\u8aad\u307f\u8fbc\u307f\u3092\u3059\u308b\u3053\u3068\u3067 UX \u306e\u5411\u4e0a\u304c\u671f\u5f85\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u3088\u3046\u306b\u3001\u9045\u5ef6\u8aad\u307f\u8fbc\u307f\u304c\u5fc5\u8981\u306a\u5834\u5408\u306b\u3082\u3001\u300cuseSWRMutation\u300d\u3092\u4f7f\u3063\u3066\u624b\u52d5\u3067\u30c7\u30fc\u30bf\u306e\u66f4\u65b0\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8: <a href=\"https:\/\/swr.vercel.app\/ja\/docs\/mutation.ja#useswrmutation\">https:\/\/swr.vercel.app\/ja\/docs\/mutation.ja#useswrmutation<\/a><\/p>\n<p>\u5f93\u6765\u306e useSWR \u304b\u3089 useSWRMutation \u3067\u3069\u306e\u3088\u3046\u306a\u9055\u3044\u304c\u3042\u308b\u306e\u304b\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3092\u4f7f\u3063\u3066\u7d39\u4ecb\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<h2>\u5f93\u6765\u306e useSWR<\/h2>\n<p>page.tsx \u3068\u3044\u3046\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3092\u4f7f\u3063\u3066\u898b\u3066\u3044\u304d\u307e\u3059\u3002<strong>page.tsx \u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u304c\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3055\u308c\u308b\u30bf\u30a4\u30df\u30f3\u30b0<\/strong> \u3067 useSWRxPage \u304c\u547c\u3070\u308c\u3001fetcher \u304c\u8d70\u308a\u30c7\u30fc\u30bf\u304c\u53d6\u5f97\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>page.tsx<\/p>\n<pre><code class=\"language-typescript\">import React, { FC } from &#039;react&#039;;\n\nimport useSWR, { SWRResponse } from &#039;swr&#039;;\n\n\/\/ swr \u306e\u30ab\u30b9\u30bf\u30e0\u30d5\u30c3\u30af\nconst useSWRxPage = (pageId: string): SWRResponse =&gt; {\n  const key = pageId;\n  return useSWR(\n    key, () =&gt; fetch(&#039;page&#039;).then(response =&gt; response),\n  );\n};\n\ntype Props = {\n  pageId: string,\n}\n\nexport const Page: FC = (props: Props) =&gt; {\n  \/\/ props \u304b\u3089 pageId \u3092\u8cb0\u3044\u3001\u30ab\u30b9\u30bf\u30e0\u30d5\u30c3\u30af\u306e\u7b2c\u4e00\u5f15\u6570\u306b\u5165\u308c\u3044\u3066\u3044\u308b\n  const { data, mutate } = useSWRxPage(props.pageId);\n\n  return (\n    &lt;&gt;\n      &lt;div&gt;\n        \/\/ \u30da\u30fc\u30b8\u306e\u672c\u6587\u3092\u8868\u793a\n        { data.body }\n      &lt;\/div&gt;\n        \/\/ \u30dc\u30bf\u30f3\u3092\u62bc\u3057\u3066\u518d fetch \n      &lt;button onClick={() =&gt; mutate()} \/&gt;\n    &lt;\/&gt;\n  );\n};<\/code><\/pre>\n<h2>swr2.0 \u3067\u8ffd\u52a0\u3055\u308c\u305f useSWRMutation<\/h2>\n<p>\u6b21\u306b useSWRMutation \u3092\u4f7f\u3063\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u4e0a\u8a18\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3092 useSWR \u304b\u3089 useSWRMutation \u3092\u4f7f\u3046\u30b3\u30fc\u30c9\u306b\u7f6e\u304d\u63db\u3048\u3066\u3044\u307e\u3059\u3002useSWRMutation \u3092\u4f7f\u3046\u3068 page.tsx \u304c\u30ec\u30f3\u30c0\u30ea\u30f3\u30b0\u3055\u308c\u305f\u6642\u70b9\u3067\u306f fetcher \u306f\u8d70\u308a\u307e\u305b\u3093\u3002<strong>trigger \u3068\u547c\u3070\u308c\u308b hook \u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u3053\u3068\u3067\u521d\u3081\u3066\u30df\u30e5\u30fc\u30c6\u30fc\u30b7\u30e7\u30f3\u304c\u884c\u308f\u308c\u307e\u3059\u3002<\/strong><\/p>\n<p>page.tsx<\/p>\n<pre><code class=\"language-typescript\">import React, { FC } from &#039;react&#039;;\n\nimport useSWRMutation, { SWRMutationResponse } from &#039;swr\/mutation&#039;;\n\n\/\/ swr \u306e\u30ab\u30b9\u30bf\u30e0\u30d5\u30c3\u30af\nconst useSWRMUTxPage = (pageId: string): SWRMutationResponse =&gt; {\n  const key = pageId;\n  return useSWRMutation(\n    key,\n    () =&gt; fetch(&#039;page&#039;).then(response =&gt; response),\n  );\n};\n\ntype Props = {\n  pageId: string,\n}\n\nexport const Page: FC = (props: Props) =&gt; {\n  const { trigger, data } = useSWRMUTxPage(props.pageId);\n\n  return (\n    &lt;&gt;\n      &lt;div&gt;\n        \/\/ trigger \u304c\u5b9f\u884c\u3055\u308c\u308b\u307e\u3067 undefined\n        { data.body }\n      &lt;\/div&gt;\n      &lt;button onClick={() =&gt; trigger()} \/&gt;\n    &lt;\/&gt;\n  );\n};<\/code><\/pre>\n<h2>\u6700\u5f8c\u306b<\/h2>\n<p>useSWRMutation \u306e\u767b\u5834\u306b\u3088\u3063\u3066 SWR \u3092\u4f7f\u3063\u305f\u3088\u308a UX \u306e\u9ad8\u3044\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u958b\u767a\u304c\u671f\u5f85\u3067\u304d\u305d\u3046..\uff1f\u3068\u3044\u3046\u304a\u8a71\u3067\u3057\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3002GROWI \u30a8\u30f3\u30b8\u30cb\u30a2\u306e \u5bae\u6ca2 \u3067\u3059\u3002\u4eca\u56de\u306f\u4fbf\u5229\u306a React hooks \u3067\u3042\u308b SWR 2.0 \u3067\u8ffd\u52a0\u3055\u308c\u305f useSWRMutation \u306e\u4f7f\u3044\u6240\u306b\u3064\u3044\u3066\u7c21\u5358\u306b\u7d39\u4ecb\u3057\u3088\u3046\u3068\u601d\u3044\u307e\u3059\u3002\u3042\u308b\u7a0b\u5ea6 SWR \u3092<\/p>\n","protected":false},"author":11,"featured_media":4143,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[150,57],"tags":[],"_links":{"self":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts\/4181"}],"collection":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/comments?post=4181"}],"version-history":[{"count":2,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts\/4181\/revisions"}],"predecessor-version":[{"id":4185,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts\/4181\/revisions\/4185"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/media\/4143"}],"wp:attachment":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/media?parent=4181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/categories?post=4181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/tags?post=4181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}