Skip to content

Instantly share code, notes, and snippets.

@gaygenius
Last active February 15, 2020 17:41
Show Gist options
  • Save gaygenius/2e960eeffa80c028e32a45f298e9e834 to your computer and use it in GitHub Desktop.
Save gaygenius/2e960eeffa80c028e32a45f298e9e834 to your computer and use it in GitHub Desktop.
Next.js with TypeScript

Next.js with TypeScript

setup

npm init next-app ~/code/next-ts && code ~/code/next-ts
npm i -D typescript @types/react @types/node
git rm -f index.js

index.tsx

import React from 'react';
import { NextPage } from 'next';

const Home: NextPage<{ userAgent: string }> = ({ userAgent }) => (
  <h1>Hello, world! — user agent: {userAgent}</h1>
);

Home.getInitialProps = async ({ req }) => {
  const userAgent = req ? req.headers['user-agent'] || '' : navigator.userAgent;
  return { userAgent };
};

export default Home;

run

npm run dev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment