質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Next.js

Next.jsは、Reactを用いたサーバサイドレンダリングなどを行う軽量なフレームワークです。Zeit社が開発しており、nextコマンドでプロジェクトを作成することにより、開発環境整備が整った環境が即時に作成できます。

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

Q&A

0回答

1124閲覧

Next.js+TypeScriptにてgetInitialPropsではなくgetServerSidePropsを使用したいが書き方が分からない。

f6ae

総合スコア92

Next.js

Next.jsは、Reactを用いたサーバサイドレンダリングなどを行う軽量なフレームワークです。Zeit社が開発しており、nextコマンドでプロジェクトを作成することにより、開発環境整備が整った環境が即時に作成できます。

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

0グッド

2クリップ

投稿2020/10/25 05:31

編集2020/10/30 10:19

質問

Next.js + TypeScriptの勉強のため以下のリポジトリの改造を試みています。
https://github.com/jasonraimondi/nextjs-jwt-example
getServerSidePropsをComponentで使用する場合の書き方が分からず詰まってしまいました。

発生している問題・エラーメッセージ

web/components/private_route.tsx

import ServerCookie from "next-cookies"; import Router from "next/router"; import React, { Component } from "react"; import { COOKIES } from "../services/login_service"; import { AuthToken } from "../services/auth_token"; export type AuthProps = { auth: AuthToken } export function privateRoute(WrappedComponent: any) { return class extends Component<AuthProps> { static async getInitialProps(ctx: any) { const token = ServerCookie(ctx)[COOKIES.authToken]; const auth = new AuthToken(token); const initialProps = { auth }; if (auth.isExpired) { ctx.res.writeHead(302, { Location: "/login?redirected=true", }); ctx.res.end(); } if (WrappedComponent.getInitialProps) return WrappedComponent.getInitialProps(initialProps); return initialProps; } get auth() { // the server pass to the client serializes the token // so we have to reinitialize the authToken class // // @see https://github.com/zeit/next.js/issues/3536 return new AuthToken(this.props.auth.token); } render() { return <WrappedComponent auth={this.auth} {...this.props} />; } }; }


Next.js9.3以降ではgetInitialPropsではなくgetServerSideProps等が推奨されるとのことから、
以下のような形に書き換えたいと思っています。

import ServerCookie from "next-cookies"; import Router from "next/router"; import React, { Component } from "react"; import { COOKIES } from "../services/login_service"; import { AuthToken } from "../services/auth_token"; import { GetServerSideProps } from "next"; export type AuthProps = { auth: AuthToken } export function privateRoute(WrappedComponent: any) { return class extends Component<AuthProps> { // static async getInitialProps(ctx: any) { // const token = ServerCookie(ctx)[COOKIES.authToken]; // const auth = new AuthToken(token); // const initialProps = { auth }; // if (auth.isExpired) { // ctx.res.writeHead(302, { // Location: "/login?redirected=true", // }); // ctx.res.end(); // } // if (WrappedComponent.getInitialProps) return WrappedComponent.getInitialProps(initialProps); // return initialProps; // } get auth() { // the server pass to the client serializes the token // so we have to reinitialize the authToken class // // @see https://github.com/zeit/next.js/issues/3536 return new AuthToken(this.props.auth.token); } render() { return <WrappedComponent auth={this.auth} {...this.props} />; } }; } export const getServerSideProps: GetServerSideProps = async (ctx : any) => { const token = ServerCookie(ctx)[COOKIES.authToken]; const auth = new AuthToken(token); const initialProps = { auth }; if (auth.isExpired) { ctx.res.writeHead(302, { Location: "/login?redirected=true", }); ctx.res.end(); } if (WrappedComponent.getInitialProps) return WrappedComponent.getInitialProps(initialProps); return initialProps; }

実際に上記の通り書き換えたところ、
(1) getServerSidePropsでreturnしているpropsをprivateRoute関数内で使用したいが、受け渡し方法が分からない。
(2) if (WrappedComponent.getInitialProps) return WrappedComponent.getInitialProps(initialProps);の「WrappedComponent」の部分で「名前 'WrappedComponent' が見つかりません。」とシンタックスエラーが出る。またgetInitialPropsではなくgetServerSidePropsへの書き換え方が分からない。
という問題が出て、詰まっています。
(1)については、公式ドキュメントの解説どおり「function Page({ data }) {」のようにすべきかと思いましたが、このコードの場合、Componentで引数としてWrappedComponentを取るため、こういう場合どのように書けば良いのか分からない状態です。
https://nextjs.org/docs/basic-features/data-fetching#simple-example-1
ちなみにこちらのComponentは、
web/pages/dashboard.tsx
などで使用されており、ログイン時にのみ見れるページを作成する為のComponentになり、Pageのコードにて「export default privateRoute(Page);」のような書き方で使用します。

質問

(1),(2)それぞれどのようにすれば解消出来るでしょうか。
Next.js TypeScript共に初心者であるため、見当違いのことをしていれば申し訳ございません。ご指摘いただければ幸いです。

試したこと

getServerSidePropsに関して公式ドキュメントを読んだり
GitHubで類似のコードを探したり書き方を数日模索しましたが、分かりませんでした。

補足情報(FW/ツールのバージョンなど)

Next.js 9.5
TypeScript 3.5

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問