npx create-next-app@latest my-next-app
cd my-next-app
npm run dev
npm run build
npm start
// pages/index.js
export default function Home() {
return <h1>Home Page</h1>
}
// pages/about.js
export default function About() {
return <h1>About Page</h1>
}
// pages/posts/[id].js
import { useRouter } from 'next/router'
export default function Post() {
const router = useRouter()
const { id } = router.query
return <p>Post: {id}</p>
}
// pages/posts/[...slug].js
import { useRouter } from 'next/router'
export default function Post() {
const router = useRouter()
const { slug } = router.query
return <p>Slug: {slug.join('/')}</p>
}
import Link from 'next/link'
export default function NavBar() {
return (
<nav>
<Link href="/">Home</Link>
<Link href="/about">About</Link>
</nav>
)
}
import { useRouter } from 'next/router'
export default function Page() {
const router = useRouter()
return (
<button onClick={() => router.push('/about')}>
Go to About
</button>
)
}
// pages/_app.js
import '../styles/globals.css'
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
export default MyApp
// pages/_document.js
import { Html, Head, Main, NextScript } from 'next/document'
export default function Document() {
return (
<Html lang="en">
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
// pages/404.js
export default function Custom404() {
return <h1>404 - Page Not Found</h1>
}
// pages/500.js
export default function Custom500() {
return <h1>500 - Server-side error occurred</h1>
}
// pages/api/hello.js
export default function handler(req, res) {
res.status(200).json({ message: 'Hello from Next.js!' })
}
// pages/api/posts/[id].js
export default function handler(req, res) {
const { id } = req.query
res.status(200).json({ id, message: `Post ${id}` })
}
export async function getServerSideProps(context) {
const res = await fetch(`https://api.example.com/data`)
const data = await res.json()
return {
props: { data },
}
}
export default function Page({ data }) {
return <div>{data.title}</div>
}
export async function getStaticProps() {
const res = await fetch(`https://api.example.com/data`)
const data = await res.json()
return {
props: { data },
revalidate: 60, // Optional: revalidate every 60 seconds
}
}
export default function Page({ data }) {
return <div>{data.title}</div>
}
export async function getStaticPaths() {
const res = await fetch('https://api.example.com/posts')
const posts = await res.json()
const paths = posts.map((post) => ({
params: { id: post.id.toString() },
}))
return { paths, fallback: false }
}
export async function getStaticProps({ params }) {
const res = await fetch(`https://api.example.com/posts/${params.id}`)
const post = await res.json()
return { props: { post } }
}
export default function Post({ post }) {
return <div>{post.title}</div>
}
// styles/Home.module.css
.container {
padding: 0 2rem;
}
// pages/index.js
import styles from '../styles/Home.module.css'
export default function Home() {
return <div className={styles.container}>Hello World</div>
}
npm install sass
// styles/globals.scss
$font-stack: Helvetica, sans-serif;
$primary-color: #333;
body {
font: 100% $font-stack;
color: $primary-color;
}
// pages/_app.js
import '../styles/globals.scss'
export default function Button() {
return (
<div>
<button>Click me</button>
<style jsx>{`
button {
background-color: blue;
color: white;
padding: 10px 20px;
}
`}</style>
</div>
)
}
import Image from 'next/image'
export default function Avatar() {
return (
<Image
src="/images/profile.jpg"
alt="Profile picture"
width={500}
height={500}
/>
)
}
import { Inter } from 'next/font/google'
const inter = Inter({ subsets: ['latin'] })
export default function MyApp({ Component, pageProps }) {
return (
<main className={inter.className}>
<Component {...pageProps} />
</main>
)
}
Place files in the public
directory:
public/
images/
profile.jpg
Access in your code:
<img src="/images/profile.jpg" alt="Profile picture" />
// .env.local
DB_HOST=localhost
DB_USER=myuser
DB_PASS=mypassword
// pages/api/db.js
export default function handler(req, res) {
res.status(200).json({
host: process.env.DB_HOST,
user: process.env.DB_USER
})
}
npm install --save-dev typescript @types/react @types/node
Rename files to use .ts
or .tsx
extension.
npm install -g vercel
vercel
next build
next export
Next.js automatically prefetches the JavaScript code for the linked pages in the background.
import dynamic from 'next/dynamic'
const DynamicComponent = dynamic(() => import('../components/hello'))
export default function Home() {
return (
<div>
<DynamicComponent />
</div>
)
}
// next.config.js
module.exports = {
i18n: {
locales: ['en', 'fr', 'de'],
defaultLocale: 'en',
},
}
import { useRouter } from 'next/router'
const dict = {
en: { greeting: 'Hello' },
fr: { greeting: 'Bonjour' },
de: { greeting: 'Hallo' },
}
export default function Page() {
const router = useRouter()
const { locale } = router
return <h1>{dict[locale].greeting}</h1>
}
npm install next-auth
// pages/api/auth/[...nextauth].js
import NextAuth from 'next-auth'
import Providers from 'next-auth/providers'
export default NextAuth({
providers: [
Providers.GitHub({
clientId: process.env.GITHUB_ID,
clientSecret: process.env.GITHUB_SECRET
}),
],
})
npm install --save-dev jest @testing-library/react @testing-library/jest-dom
// __tests__/index.test.js
import { render, screen } from '@testing-library/react'
import Home from '../pages/index'
describe('Home', () => {
it('renders a heading', () => {
render(<Home />)
const heading = screen.getByRole('heading', {
name: /welcome to next.js!/i,
})
expect(heading).toBeInTheDocument()
})
})
Run tests:
npm test
2024 © All rights reserved - buraxta.com