Sample Next.js Project
Last updated
Last updated
In this page, we’ll make a quick introduction to Next.js
and build a simple page using Next.js
, Material UI, and the fetch API.
Here will be the result of this tutorial.
We have already seen how to create a Next.js
project. Inside the newly created project, run the following command to run the development server of the project.
Once the project is running, we can install the needed packages. We need to install the Material UI library using
yarn`.
After the installation, we need to configure the project for the data we will use. In the public
directory, create a file called products.json
.
This file will contain a list of product objects with data we’ll display to the user using Next.js
and Material UI. You can have an example of this file here.
We need to create the product.json file in the public
directory, so it can be served from the server and we’ll be able to fetch the data using the fetch API. After adding the products.json
file, create a new file named product.js
in pages/api/
directory. This file will contain a function we’ll write to fetch data from the server at http://localhost:3000/products.json
.
The function to fetch data is ready now, we can write the Product
component to display information about the products.
In the pages
directory, create a new directory called components
. This directory will contain the components for this project.
Inside the newly created directory, create a new file called Product.jsx
. This file will contain the code for the Product component. We’ll use Material UI to build a simple card that will display the name, the image, and the price of the product.
The Product
component should receive product
props to work properly. As said earlier, we are using Material UI components to create a card and thankfully, MUI provides components for Typography, Card content, and Card image. This makes the task much easier.
We will use the Product
component in the index.js
file in the pages
directory. It represents the main page of the application.
Let’s start with importing the getProducts
function and initiate some important states that will be used on the page.
Here we are defining the products
state, which will normally contain the list of the product objects. After that, we use the useEffect
hook to make some computations before the component is mounted. We are basically calling the getProducts
function and retrieving the response.
Let’s through this response now and display the products.
Open the page at http://localhost:3000/
, and you will see the result. And voilà , we have successfully created a simple project with Next.js
.
The next documentation will concern how to deploy a Next.js
application on Netlify. In the meantime, you can find the code for this sample project here.