Cover image for Day 11 of #100DaysOfCode: The basis of Next.js - Pages, Link, and Meta

Day 11 of #100DaysOfCode: The basis of Next.js - Pages, Link, and Meta

jenhsuan profile image Jen-Hsuan Hsieh Updated on ・3 min read


It's easy to use Next.js to write a React application. There are a few basic feature for Next.js.

  • Create Next.js App
  • Pages
  • Link
  • Fetching data
  • Meta in pages

Create Next.js App


npm init
npm install --save react react-dom next
mkdir pages

2.Edit package.json

"scripts": {
    "build":"next build",
    "start":"next start"

3.Create pages/index.js

const Index = () => (<h1>hello</h1>)
export default Index;


npm run dev

5.Open browser and visit http://localhost:3000

Setting up for TypeScript

1.Create tsconfig.json

touch tsconfig.json


npm run dev


In Next.js, a page is a React Component exported from a .js, jsx, .ts, or .tsx file in the pages directory. Each page is associated with a route based on its file name.
Example: If you create pages/about.js that exports a React component like below, it will be accessible at /about.
source: https://nextjs.org/docs/basic-features/pages

  • Create about.js
function About() {
  return <div>About</div>
export default About


import Link from 'next/link'
const Index = () => (
        <link href='/about'>
            <a> about </a>
export default Index;

Fetching data

1.Use getInitialProps to fetch data then pass data from props
2.Use next/router to rerender the page to call getInitialProps again

import fetch from "isomorphic-fetch";
import {useState} from 'react';
import Router from 'next/router'

const Index = ({news}) => {
    const [value, setValue] = useState({
    const {text1, text2} = value;

    const handleChange = name => e => {
      setValue({...value, [name]: e.target.value})

    const handleSubmit = e => {

    const searchForm = () => (
        <form onSubmit = {handleSubmit}>
              type = "text" 
              onChange = {handleChange('text1')}/>
              type = "text" 
              onChange = {handleTextChange('text2')}/>

    return (

Index.getInitialProps = async ({query}) => {
  let news;
  try {
    const res = await fetch(`.../search?query=${query.searchTerm}`);
    news = await res.json();
  } catch (err) {
    news = [];

  return {
export default Index;

Shallow routing

Shallow routing allows you to change the URL without running data fetching methods again, that includes getServerSideProps, getStaticProps, and getInitialProps. To enable shallow routing, set the shallow option to true. Consider the following

import { useEffect } from 'react'
import { useRouter } from 'next/router'

// Current URL is '/'
function Page() {
  const router = useRouter()

  useEffect(() => {
    // Always do navigations after the first render
    router.push('/?counter=10', undefined, { shallow: true })
  }, [])

  useEffect(() => {
    // The counter changed!
  }, [router.query.counter])

export default Page

Meta in pages

  • Head  
import Head from 'next/head'
    <meta name="des" content="..."/>
    <meta name="keywords" content="seo"/>
    <meta name="author" content="..."/>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
  • Document
import Document, {Html, Head, Main, NextScript} from 'next/document'
class MyDocument extends Document {     
    render() {         
        return (             
                    <meta name="des" content="..."/>
                    <meta name="keywords" content="seo"/>
                    <meta name="author" content="..."/>      
                    <Main />                     
                    <NextScript />                 
export default MyDocument


There are some of my articles. Feel free to check if you like!

Posted on by:

jenhsuan profile

Jen-Hsuan Hsieh


6+ year work experience in the software engineering field. 2+ year work experience with front-end JavaScript framework like React.js, Knockout.js. and Microsoft solution.


markdown guide