Giter VIP home page Giter VIP logo

nextjs-overview's Introduction

NextJS overview

start

npx create-next-app@latest

yarn dev

Routing

import Link from 'next/link'

  <Link href="/">
    <a>Home</a>
  </Link>

  <Link href="/about">
    <a>Home</a>
  </Link>

styled jsx

<nav >
  <Link href="/">
    <a className={router.pathname === '/' ? 'active' :''}>Home</a>
  </Link>
  <Link href="/about">
    <a className={router.pathname === '/about' ? 'active' :''}>About</a>
  </Link>
    

  <style jsx>
  { `
     nav {
       background-color:tomato;
        }
     a {
       text-decoration:none;
     }
     .active {
       color:blue;
     }
  `}
  </style>
</nav>

스타일들은 해당 컴포넌트 내부로 범위가 한정되어 다른 컴포넌트에 영향을 주지 않음.

_app.js

 function App({Component,pageProps}){
    return (
     <Component {...pageProps}/>
    )
 export default App

_app은 서버로 요청이 들어왔을 때 가장 먼저 실행되는 컴포넌트로, 페이지에 적용할 공통 레이아웃의 역할을 함.

redirect

async redirects(){
    return [
      {
        source:"/contact",
        destination:"/form",
        permanent:false,
      }
    ]
  }

rewrite

async rewrites(){
    return [
      {
        source : "/api/movies",
        destination:`https://api.themoviedb.org/3/movie/popular?api_key=${API_KEY}`
      }
    ]
  }
}

Server Side Rendering

export async function getServerSideProps() {
    const { results } = await (
      await fetch(`http://localhost:3000/api/movies`)
    ).json();
    return {
      props: {
        results,
      },
    };
  }

page에 props로 넘겨줌

Dynamic URL

import { useRouter } from "next/router";

export default function Detail(){
    const router = useRouter()
    console.log(router);
    return <div>
        <h4>{router.query.title || "Loading"} </h4>
    </div>
}

page폴더 안에 []로 감싸진 파일 생성

url masking

  router.push({
            pathname:`/movies/${id}`,
            query:{
                title:"potato"
            }
        },`/movies/${id}`)

nextjs-overview's People

Contributors

bangbongbim avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.