propella / simple-next

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Next.js / TypeScript を簡単に始める手順

https://qiita.com/propella/items/e2b406cda8266dcd2974

手軽に React.js を使った実験をしたいので、設定無しで使える Next.js の利用開始手順をまとめた。(https://github.com/propella/simple-next)

手順

next と依存ライブラリのインストール

npm install react react-dom next
npm install --save-dev typescript @types/react @types/node eslint

空の tsconfig.json を作る

touch tsconfig.json

pages/index.tsx を書く。パス名は決まっていて、pages/hoge.tsx のように tsx を置くと http://localhost/hoge からアクセスできる。対応する html は Next.js が自分で作る。

export default function Home() {
  return <p>Hello, World!</p>
}

package.json に起動スクリプト追加

  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },

テスト実行するとおすすめの tsconfig.json が作られる。

npm run dev

http://localhost:3000 を開く

tsconfig.json にエラーがあるので compilerOptions に以下を追加。またお好みで "strict": true したりする。

"moduleResolution": "node",

lint の設定

npm run lint

Pre-rendering

利用する関数によって2つのレンダリング方式を選べる。

npm run build && npm start するとレンダリング方式の違いを確認できる。Next.js は生の React.js と違いサーバ側に Node やロードバランサ等が必須なので、Vercel のようなビジネスが成立する。

Export

npx next build && npx next export

で生の React のように静的なアプリを生成するので、従来型の SPA を作れる。この場合 getServerSideProps のような実行時に Node サーバーが必要な機能が使われているとエラーになる。

参考

About


Languages

Language:TypeScript 100.0%