next.js

Hello Next.js !

ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

yarn init

yarn add next react react-dom

yarn dev //๋จผ์ € ์•„๋ž˜ ๋ฌธ๊ตฌ๋ฅผ ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

pakage.json์— ๋‹ค์Œ ์ถ”๊ฐ€

"scripts": {
    "dev": "next"
}

pages

pagesํด๋”์•ˆ์— index.js์ƒ์„ฑ

๋ฐ˜๋“œ์‹œ next.js๋Š” pagesํด๋” ์•ˆ์— ์žˆ์–ด์•ผ๋งŒ ํ•ฉ๋‹ˆ๋‹ค.(์ฝ”๋“œ์Šคํ”Œ๋ฆฟํŒ…)

import React from "react"; //์žˆ์–ด๋„ ์—†์–ด๋„ ๋ฌด๊ด€

export default function index() {
  return <div>Hello.next</div>;
}

ํŽ˜์ด์ง€์™€ ๋ ˆ์ด์•„์›ƒ

page

pages ํด๋”์•ˆ์— ํŽ˜์ด์ง€๋ฅผ ํ•˜๋‚˜์”ฉ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

import React from "react";

export default function profile() {
  return <div>๋‚ด ํ”„๋กœํ•„</div>;
}

๊ฐœ๋ฐœ ์„œ๋ฒ„ti๋ฅผ ๋‹ค์‹œ ์ผœ๋ณด๋ฉด ํŒŒ์ผ๋ช…๊ณผ ๋งค์นญ๋˜์–ด์„œ react-router์˜ ๊ธฐ๋Šฅ์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋งŒ์•ฝ ํŽ˜์ด์ง€๋ช…์„ ๋ฐ”๊พธ๊ณ  ์‹ถ๋‹ค๋ฉด ํŒŒ์ผ๋ช…์„ [profile].js ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค๋ฉด ๋ฉ๋‹ˆ๋‹ค.

Components

๋ฆฌ์•กํŠธ์— ์‚ฌ์šฉํ–ˆ๋˜ ์žฌ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•œ ์ปดํฌ๋„ŒํŠธ๋Š” ๋”ฐ๋กœ ํด๋”๋ฅผ ๋งŒ๋“ค์–ด ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์ด ๋ง์ด์ฃ 

import React from "react";

export default function AppLayout({ children }) {
  return return (
    <div>
      <h1>๊ณตํ†ต๋ฉ”๋‰ด</h1>
      {children}
    </div>
  );
}

๋ฌผ๋ก  Link ๊ธฐ๋Šฅ๋„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

import React from "react";
import Link from "next/Link"; //importํ•ด์ฃผ๊ธฐ

export default function AppLayout({ children }) {
  return (
    <div>
      <Link href="/">
        <a>Conrad</a>
      </Link>
      <Link href="/profile">
        {/* Linkํƒœ๊ทธ ์•ˆ์—์„œ href ํ•ด์ค€๋’ค ๋‚ด๋ถ€์— aํƒœ๊ทธ */}
        <a>ํ”„๋กœํ•„</a>
      </Link>
      <Link href="/signup">
        <a>ํšŒ์›๊ฐ€์ž…</a>
      </Link>
      {children}
    </div>
  );
}

๊ฐ„๋‹จํ•˜๊ฒŒ ๋„ค๋น„๊ฒŒ์ด์…˜์„ ๋งŒ๋“ค์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

์ฒ˜์Œ ๋นŒ๋“œํ•˜๋ฉด Link์ด๋™์‹œ ๋”œ๋ ˆ์ด๊ฐ€ ์žˆ๋Š”๋ฐ, ์ด๋Š” ๊ฐœ๋ฐœ๋ชจ๋“œ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

production๋ชจ๋“œ๋กœ ๋ณ€ํ™˜๋˜๋ฉด ๋”œ๋ ˆ์ด๊ฐ€ ์—†์–ด์ง€๋‹ˆ ๊ฑฑ์ • ์•ˆํ•ด๋„๋ฉ๋‹ˆ๋‹ค.

esLint

์ด๋ฒˆ์—” esLint๋ฅผ ์ ์šฉ์‹œ์ผœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋จผ์ € ์„ค์น˜๋ฅผ ํ•ด์ฃผ์„ธ์š”

yarn add eslint -D
yarn add eslint-plugin-react -D //์ฝ”๋“œ์ ๊ฒ€์šฉ
yarn add react-hooks -D 

๋‹ค์Œ์—๋Š” .eslintrc ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”

{
  "parserOption": {
    "ecmaVersion": 2020,
    "sourceType": "module",
    "ecmaFeature": {
      "jsx": true
    }
  },
  "env": {
    "browser": true,
    "node": true,
    "es6": true
  },
  "extends": ["eslint:recommend", "plugin:react/recommend"], //๋„ˆ๊ทธ๋Ÿฝ๋‹ค..
  "plugins": ["import", "react-hooks"]
}

Last updated