"A beginner-friendly guide to Next.js, covering its essential features, latest updates, and why it’s a go-to choice for modern web development."
By Samir Niroula
28 October 2024Next.js is a powerful framework built on top of React that enhances the development experience by offering server-side rendering (SSR), static site generation (SSG), and many other advanced features. Created by Vercel, Next.js is known for simplifying React applications, improving SEO, and enhancing page load speed.
In this post, we'll dive into the basics of Next.js, including the latest features, and walk through how to get started.
Next.js is a popular choice for developers because it combines the best of React with additional optimizations, making it easy to build fast, production-ready applications. Here’s why developers love it:
To create a new Next.js project, ensure you have Node.js installed. Then, run:
npx create-next-app@latest my-next-app
cd my-next-app
npm run dev
This starts a development server at http://localhost:3000, where you can see your Next.js application running.
In Next.js, each file in the pages
directory automatically becomes a route. For example, creating a file about.js
in the pages
folder makes the route accessible at /about
.
// pages/index.js
export default function Home() {
return <h1>Welcome to My Next.js Site!</h1>;
}
// pages/about.js
export default function About() {
return <h1>About Us</h1>;
}
Next.js has seen some exciting updates. Here are the highlights of the latest features for beginners:
The App Router introduced in Next.js 13 provides more flexibility with layouts, loading states, and simplified data fetching.
// app/layout.js
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
Server Components allow components to run on the server by default, reducing client-side JavaScript and improving performance.
With API routes, you can build back-end functionality in your Next.js app. Create a file in the pages/api
folder to set up a new endpoint.
// pages/api/hello.js
export default function handler(req, res) {
res.status(200).json({ message: "Hello, World!" });
}
Access this at /api/hello
.
Next.js supports different data-fetching methods:
getStaticProps
: Fetches data at build time for static pages.getServerSideProps
: Fetches data on each request for server-rendered pages.getInitialProps
: Legacy option, less commonly used now.getStaticProps
// pages/posts.js
export async function getStaticProps() {
const res = await fetch("https://jsonplaceholder.typicode.com/posts");
const posts = await res.json();
return { props: { posts } };
}
export default function Posts({ posts }) {
return (
<div>
<h1>Posts</h1>
<ul>
{posts.map((post) => (
<li key={post.id}>{post.title}</li>
))}
</ul>
</div>
);
}
Vercel is the creator of Next.js and offers seamless deployment for Next.js apps. To deploy:
Next.js provides a robust, feature-rich environment for React developers. Whether you’re building a personal blog, an e-commerce platform, or a complex web application, Next.js’s tools and optimizations make development efficient and enjoyable. Happy coding!