warizz.io

React, NextJs and Typescript: What is correct type for a page

April 19, 2020

This code comes from NextJs’ documentation and still valid for latest stable next package

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;

Warizz Yutanan

Written by Warizz Yutanan, a software developer who try to live a happy life