{"id":1966,"date":"2022-06-05T13:13:41","date_gmt":"2022-06-05T04:13:41","guid":{"rendered":"http:\/\/www.agile-software.site\/?p=1966"},"modified":"2022-07-16T16:39:09","modified_gmt":"2022-07-16T07:39:09","slug":"reactgetserversideprops","status":"publish","type":"post","link":"https:\/\/agile-software.net\/?p=1966","title":{"rendered":"React(getServerSideProps)"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"toc_headline_1\">context<\/h2>\n\n\n\n<p>\u307e\u305a\u3001<code>GetServerSideProps<\/code>&nbsp;\u95a2\u6570\u306f\u3001\u6b21\u306e\u3088\u3046\u306b\u5f15\u6570\u3092\uff11\u3064\u3068\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-js\" data-file=\"JavaScript \u306e\u5834\u5408\" data-lang=\"JavaScript\"><code>export async function getServerSideProps(context){\n\t\/\/\u51e6\u7406\n}<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-ts\" data-file=\"TypeScript \u306e\u5834\u5408\" data-lang=\"TypeScript\"><code>export const getServerSideProps: GetServerSideProps = async (context) =&gt; {\n\t\/\/\u51e6\u7406\n}<\/code><\/pre><\/div>\n\n\n\n<p>\u901a\u5e38&nbsp;<code>context<\/code>&nbsp;\u3068\u540d\u4ed8\u3051\u308b\u3053\u306e\u5909\u6570\u306b\u306f\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u3084\u30ec\u30b9\u30dd\u30f3\u30b9\u3001\u30af\u30a8\u30ea\u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u60c5\u5831\u306a\u3069\u304c\u683c\u7d0d\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>params\uff1a\u3053\u306e\u30da\u30fc\u30b8\u304c\u52d5\u7684\u30eb\u30fc\u30c8\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u306e\u30eb\u30fc\u30c8\u30d1\u30e9\u30e1\u30fc\u30bf\u304c\u542b\u307e\u308c\u307e\u3059\u3002\u30da\u30fc\u30b8\u540d\u304c[id].js\u3001\u306e\u5834\u5408\u3001\u306f\u306eparams\u3088\u3046\u306b\u306a\u308a\u307e\u3059{ id: \u2026 }\u3002<br>req\uff1a\u30ea\u30af\u30a8\u30b9\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8<br>res\uff1a\u5fdc\u7b54\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3002<br>query\uff1a\u30af\u30a8\u30ea\u6587\u5b57\u5217\u3092\u8868\u3059\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_headline_2\">\u30af\u30a8\u30ea\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u53d7\u3051\u53d6\u308b<\/h2>\n\n\n\n<p><code>GetServerSideProps<\/code>&nbsp;\u3067\u30af\u30a8\u30ea\u30d1\u30e9\u30e1\u30fc\u30bf\u306f\u3001\u5f15\u6570&nbsp;<code>context<\/code>&nbsp;\u306e&nbsp;<code>query<\/code>&nbsp;\u30ad\u30fc\u306b\u683c\u7d0d\u3055\u308c\u3066\u3044\u308b\u305f\u3081\u3001\u3053\u3053\u304b\u3089\u53d6\u308a\u51fa\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>export async function getServerSideProps(context){\n\n  \/\/ \u30af\u30a8\u30ea\u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u53d6\u5f97\n  const keyword = context.query.keyword;\n  const page = context.query.page;\n\n  console.log(&quot;keyword =&quot;, keyword);\n  console.log(&quot;page =&quot;, page);\n  return { props: {} };\n}<\/code><\/pre><\/div>\n\n\n\n<p>\u3053\u306e\u72b6\u614b\u3067\u3001<code>http:\/\/localhost:3000\/pagename?keyword=react&amp;page=2<\/code>&nbsp;\u306e\u3088\u3046\u306a URL \u3067\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u3001<code>getServerSideProps<\/code>&nbsp;\u306e\u4e2d\u3067\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u53d7\u3051\u53d6\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_headline_3\">POST \u30c7\u30fc\u30bf\u3092\u53d7\u3051\u53d6\u308b<\/h2>\n\n\n\n<p>Next.js \u306e\u958b\u767a\u3067\u3001\u3042\u307e\u308a\u4f7f\u7528\u3055\u308c\u308b\u3053\u3068\u306f\u306a\u3044\u3068\u601d\u3046\u304c\u3001<code>GetServerSideProps<\/code>&nbsp;\u3067 POST \u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u53d7\u3051\u53d6\u308b\u5834\u5408\u306f\u3001\u6b21\u306e\u3088\u3046\u306b\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u672c\u6587\u3092\u8aad\u307f\u53d6\u308b\u524d\u306b&nbsp;<code>bodyParser<\/code>&nbsp;\u3092\u4f7f\u7528\u3057\u3066\u4e8b\u524d\u306b\u89e3\u8aad\u3057\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u308b\u3002<\/p>\n\n\n\n<p>\u89e3\u8aad\u3057\u305f\u5f8c\u306f\u3001\u666e\u901a\u306b&nbsp;<code>req.body.&lt;\u30ad\u30fc&gt;<\/code>&nbsp;\u3067 POST \u30c7\u30fc\u30bf\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>import bodyParser from &quot;body-parser&quot;;\nimport { promisify } from &quot;util&quot;;\n\nconst getBody = promisify(bodyParser.urlencoded());\n\nexport async function getServerSideProps({ req, res }) {\n  if (req.method === &quot;POST&quot;) {\n    await getBody(req, res);\n  }\n\n  return {\n    props: {\n      name: req.body?.name || &quot;smeijer&quot;,\n      message: req.body ? &quot;received!&quot; : &quot;&quot;,\n    }\n  };\n}<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u304a\u307e\u3051\uff08getServerSideProps \u304b\u3089 API \u3092\u547c\u3076\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\u540c\u3058\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u3067\u52d5\u304f Next.js \u306e API \uff08pages\/api \u306e\u4e0b\u306e\u3084\u3064\u3089\uff09\u3092 ServerSideProps \u304b\u3089\u547c\u3073\u51fa\u3059\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u3066\u304a\u304f\u3002\n\n\u666e\u901a\u306b fetch(\"http:\/\/xxxxx\") \u306e\u3088\u3046\u306b\u3001HTTP \u30ea\u30af\u30a8\u30b9\u30c8\u3092\u767a\u884c\u3057\u3066 API \u3092\u53e9\u3044\u3066\u3082\u3044\u3044\u304c\u3001\u540c\u3058\u30b5\u30fc\u30d0\u30fc\u4e0a\u306e\u51e6\u7406\u306a\u306e\u306b\u3001\u308f\u3056\u308f\u3056 HTTP \u3067\u901a\u4fe1\u3059\u308b\u306e\u306f\u7121\u99c4\u3067\u3042\u308b\u3002\n\nServerSideProps \u304b\u3089 API \u3092\u547c\u3073\u51fa\u3059\u5834\u5408\u306f\u3001\u666e\u901a\u306b import \u3057\u3066\u547c\u3073\u51fa\u305b\u3070\u826f\u3044\u3002\n\n\u4ee5\u4e0b\u306f\u3001pages\/api\/books.ts \u306e API \u3092 ServerSideProps \u304b\u3089\u547c\u3073\u51fa\u3059\u4f8b\u3067\u3042\u308b\u3002\n<\/code><\/pre>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\" data-file=\"pages\/api\/books.ts\"><code>import type { NextApiRequest, NextApiResponse } from &#39;next&#39;\n\n\/\/ google books api \u304b\u3089\u53d6\u5f97\u3057\u305f\u60c5\u5831\u306e\u7c21\u6613\u7248\u30a4\u30f3\u30bf\u30d5\u30a7\u30fc\u30b9\nexport interface Book {\n  id: string,\n  title: string,\n  pageCount: number | null\n}\n\n\/**\n * Google Books API \u3067\u9b3c\u6ec5\u95a2\u9023\u306e\u66f8\u7c4d\u3092\u691c\u7d22\u3059\u308b\u95a2\u6570\n * [GET]\/api\/books\n * \n * @returns \u898b\u3064\u304b\u3063\u305f\u66f8\u7c4d\u306e\u7c21\u6613\u7248\u30ea\u30b9\u30c8\n *\/\nexport async function getData() : Promise&lt;Book[]&gt; {\n  const response = await fetch(&quot;https:\/\/www.googleapis.com\/books\/v1\/volumes?q=&quot; + encodeURIComponent(&quot;\u9b3c\u6ec5&quot;))\n  const jsonData = await response.json()\n  return jsonData.items.map((elem: any) =&gt; {\n    return {\n      id: elem.id,\n      title: elem.volumeInfo.title,\n      pageCount: elem.pageCount || null\n    }\n  });\n}\n\n\/**\n * Next.js \u306e API \u5b9a\u7fa9\n * \n * @param req \u30ea\u30af\u30a8\u30b9\u30c8\n * @param res \u30ec\u30b9\u30dd\u30f3\u30b9\n *\/\nexport default async function handler(\n  req: NextApiRequest,\n  res: NextApiResponse&lt;Book[]&gt;\n) {\n  const data = await getData();\n  res.status(200).json(data);\n}<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\" data-file=\"ServerSideProps\"><code>import { Book, getData } from  &#39;.\/api\/books&#39;;\n\nexport const getServerSideProps: GetServerSideProps = async (context) =&gt; {\n  const books: Book[] = await getData();\n  return { props: { items: books} };\n}<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>context \u307e\u305a\u3001GetServerSideProps\u95a2\u6570\u306f\u3001\u6b21\u306e\u3088\u3046\u306b\u5f15\u6570\u3092\uff11\u3064\u3068\u308a\u307e\u3059\u3002 \u901a\u5e38context\u3068\u540d\u4ed8\u3051\u308b\u3053\u306e\u5909\u6570\u306b\u306f\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u3084\u30ec\u30b9\u30dd\u30f3\u30b9\u3001\u30af\u30a8\u30ea\u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u60c5<\/p>\n","protected":false},"author":1,"featured_media":2031,"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-1966","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\/1966","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=1966"}],"version-history":[{"count":2,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts\/1966\/revisions"}],"predecessor-version":[{"id":2032,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts\/1966\/revisions\/2032"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/media\/2031"}],"wp:attachment":[{"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}