Next.js
React Framework
Static Pre Redering
next.js์ ์ํด ์ด๊ธฐ์ํ๋ก HTML์ด ๋ฏธ๋ฆฌ ๋ ๋๋ง๋จ
pages
file name = url route name
index = home page
component name์ ์ค์ํ์ง ์์
404 page๋ฅผ ์ ๊ณต
homepage/movies
=pages/movies/index.js
ํด๋ ๊ตฌ์กฐ์ ๋ฐ๋ผ ๋งตํํ์ด์ง๊ฐ ํ๋๋ผ๋ฉด ํด๋๋ฅผ ๋ง๋ค ํ์ ์์
routing
routing๊ณต์๋ฌธ์ router๊ณต์๋ฌธ์
Link component ์กด์ฌ
Link is only for 'href'
className, style ... a tag์ ์์ฑ
<nav>
<Link href="/">
<a>Home</a>
</Link>
<Link href="/about">
<a>About</a>
</Link>
</nav>
or
const router = useRouter();
const onClick = (id: number) => {
router.push(`/movies/${id}`);
};
or
const router = useRouter();
const onClick = (id: number, title: string) => {
router.push(
{
pathname: `/movies/${id}`,
query: {
title,
},
},
`/movies/${id}`
);
};
Dynamic routes
homepage/movies/:id
=pages/movies/[id].js
Styled-jsx
ํด๋น ์ปดํฌ๋ํธ ์ค์ฝํ ๋ด๋ก ์ ์ฉ๋ฒ์๊ฐ ํ์ ๋จ
<style jsx>{`
nav {
background-color: tomato;
}
a {
text-decoration: none;
}
`}</style>
// global๋ก ์ ์ฉ(ํ์์ปดํฌ๋ํธ๊น์ง ์ ์ฉ)
// but, ํ์ด์ง๊ฐ ๋ค๋ฅด๋ฉด ์ ์ฉ์๋จ
<style jsx global>{`
nav {
background-color: tomato;
}
a {
text-decoration: none;
}
`}</style>
CSS Modules
random class name์ ๋ง๋ค์ด์ค
// 1
import styles from "./NavBar.module.css";
const NavBar = () => {
const router = useRouter();
return (
<nav className={styles.nav}>
...
// ์ฌ๋ฌ ํด๋์ค๋ค์์ ์ ์ฉํ ๋
// 2
<Link href="/">
<a
className={`${styles.link} ${
router.pathname === "/" ? styles.active : ""
}`}
>
Home
</a>
</Link>
// 3
<Link href="/">
<a
className={[
styles.link,
router.pathname === "/" ? styles.active : "",
].join(" ")}
>
Home
</a>
</Link>
Custom App
์ฌ๊ธฐ์ GlobalStyles๋ฅผ ์ถ๊ฐํด์ค์ ์์
custom app์์๋ง css import๊ฐ ๊ฐ๋ฅ(๋ค๋ฅธ ํ์ด์ง๋ ์ปดํฌ๋ํธ์์๋ module์ด์ด์ผํจ)
// Component => ๋ ๋๋งํ๊ธธ ์ํ๋ ํ์ด์ง
import type { AppProps } from 'next/app'
const App = ({ Component, pageProps }: AppProps) => {
return (
<>
<NavBar />
<Component {...pageProps} />
</>
);
};
Layout
_app ํ์ผ์ด ๋ฌด๊ฑฐ์์ง์ง ์๋๋ก Layout์ ํ์ฉ
// components/Layout
import NavBar from "./NavBar";
type LayoutProps = {
children: React.ReactNode;
};
export default function Layout({ children }: LayoutProps) {
return (
<>
<NavBar />
<div>{children}</div>
</>
);
}
// _app
const App = ({ Component, pageProps }: AppProps) => {
return (
<Layout>
<Component {...pageProps} />
</Layout>
);
};
Redirects
Rewrite
Server Side Rendering
getServerSideProps()
: ์๋ฒ์ธก์์๋ง ์คํ๋๋ ์ฝ๋
Last updated