Introduction
Next.js is a popular React framework for building server-rendered applications. With built-in features like automatic code splitting, server-side rendering, and static site generation, Next.js makes it easier to create scalable, performant, and SEO-friendly web applications.
In this tutorial, I walk you through the basics of Next.js and build a simple blog application. By the end of this tutorial, you'll have a better understanding of how Next.js works and why it's a great choice for your next project.
Prerequisites
Before you start, make sure you have the following installed on your machine:
-
Node.js (version 12 or higher)
-
npm or yarn (package manager)
Step 1: Setting up a Next.js project
To create a new Next.js project, open your terminal and run the following command:
npx create-next-app your-blog-name
Replace your-blog-name
with your desired project name. This command will create a new Next.js project in a directory with the specified name.
Once the installation is complete,
navigate to your project directory:
cd your-blog-name
Now, start the development server:
npm run dev
Open your browser and go to http://localhost:3000
You should see the default Next.js starter page.
Step 2: Creating pages
To create a new page, simply add a new file inside the pages
directory with a .js
extension. The file name will be used as the route for that page.
For our blog, let's create an index.js
file inside the pages
directory with the following content:
import React from 'react';
const HomePage = () => {
return (
<div>
<h1>Welcome to our blog!</h1>
</div>
);
};
export default HomePage;
Save the file and refresh your browser. You should see the "Welcome to our blog!" heading on the homepage.
Step 3: Creating a blog post component
To display blog posts, create a new components
directory at the root level of your project. Inside the components
directory, create a BlogPost.js
file with the following content:
import React from 'react';
const BlogPost = ({ title, content }) => {
return (
<div>
<h2>{title}</h2>
<p>{content}</p>
</div>
);
};
export default BlogPost;
This is a simple functional component that receives title
and content as props and displays them.
Step 4: Displaying blog posts on the homepage
Update the index.js
file inside the pages
directory to import the BlogPost
component and display a list of blog posts:
import React from 'react';
import BlogPost from '../components/BlogPost';
const blogPosts = [
{ title: 'First Post', content: 'This is the first blog post.' },
{ title: 'Second Post', content: 'This is the second blog post.' },
];
const HomePage = () => {
return (
<div>
<h1>Welcome to our blog!</h1>
{blogPosts.map((post, index) => (
<BlogPost key={
Now, import the Header
component in your index.js
file and include it at the top of the HomePage
component:
import React from 'react';
import BlogPost from '../components/BlogPost';
import Header from '../components/Header';
// ... rest of the code
const HomePage = () => {
return (
<div>
<Header />
<h1>Welcome to our blog!</h1>
{blogPosts.map((post, index) => (
<BlogPost key={index} title={post.title} content={post.content} />
))}
</div>
);
};
export default HomePage;
Finally, add some basic styles to your project. Create a styles
folder inside your project root and a global.css
file inside the styles
folder:
body {
font-family: Arial, sans-serif;
line-height: 1.6;
}
header {
background-color: #f5f5f5;
padding: 1rem 0;
text-align: center;
}
h1 {
font-size: 2rem;
}
h2 {
font-size: 1.5rem;
}
p {
font-size: 1rem;
}
.blog-post {
margin-bottom: 2rem;
}
Import the global.css
file in your _app.js
file:
import '../styles/global.css';
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />;
}
export default MyApp;
Save your changes and refresh the browser. You should see the updated styles and header on your blog.
Conclusion
Congratulations! You've successfully built a simple blog application using Next.js. This tutorial covered the basics of setting up a Next.js project, creating pages and components, and adding some basic styles. Next.js offers many more features that you can explore, such as dynamic routing, API routes, and static site generation.
To learn more about Next.js, check out the official documentation here.