Next.JS

How to Iterate Array Object for Travel Package List Using React.js

How to use array iteration to dynamically render React component for Travel guide package list

9/16/2024
0 views

In any web application, especially travel-related apps, displaying a list of items such as travel packages. When working with React and Next.js, you'll frequently deal with array objects containing data that needs to be displayed dynamically in the UI. In this article, we will explore how to iterate an array of travel packages and display the data using Next.js.

1. Create an Array of Travel Packages

Here I created an array object that holds information about different travel packages.

const packages = [
{
bgImg: "venice.jpg",
name: "City of Canal",
location: "Venice",
price: "$1230.00",
},
{
bgImg: "moscow.jpg",
name: "The Heritage City",
location: "Moscow",
price: "$1430.00",
},
{
bgImg: "paris.jpg",
name: "City of Love",
location: "Paris",
price: "$1030.00",
},
{
bgImg: "mumbai.jpg",
name: "City of Dreams",
location: "Mumbai",
price: "$830.00",
},
{
bgImg: "tokyo.jpg",
name: "City of Anime",
location: "Tokyo",
price: "$2830.00",
},
{
bgImg: "beijing.jpg",
name: "The Forbidden City",
location: "Beijing",
price: "$1930.00",
},
];

2. Creating a React Component to Display Travel Packages

Next, I’ll create a functional React component to display the list of travel packages. The goal is to iterate over the packages array and render each item in a card format by calling the PackageDetails component.

import { Package } from "@/models/package";

const PackageDetails = ({ pkgInfo }: { pkgInfo: Package }) => {
return (
<aside
className="group w-1/3 bg-cover bg-center"
style={{ backgroundImage: `url(/${pkgInfo.bgImg})` }}
>
<div className="opacity-0 group-hover:opacity-100 transition-all delay-150 bg-black bg-opacity-75 w-full h-[550px] flex justify-center items-center flex-col">
<h3 className="uppercase text-center text-4xl font-semibold text-white">
{pkgInfo.name}
<br />
<small className="text-xl tracking-widest font-normal">
{pkgInfo.location}
</small>
</h3>
<h4 className="text-3xl my-6 text-white font-semibold">
{pkgInfo.price}
</h4>
<a
href=""
className="bg-green text-white font-semibold px-5 py-1.5 rounded-full"
>
Book Now
</a>
</div>
</aside>
);
};

export default PackageDetails;

3. Array Iteration using map()

Now, iterate our packages array object using map() function to get individual object and pass it to PackageDetails component using props.

<div className="flex flex-wrap mt-8">
{packages.map((pkg: Package, i: number) => {
return <PackageDetails key={i} pkgInfo={pkg} />;
})}
</div>

Explanation of Code

  • I have defined an array of travel packages that contains the data for each package (package image and location).

  • Using the map() function, I iterate over this array and create a card for each package.

  • Each card includes an image and location about the travel package.

  • A key is assigned to each card using packages array index to help React efficiently manage updates and rendering.

Conclusion

In this article, I covered how to iterate an array object and display a list of travel packages using Next.js and React.js components. The example illustrated how to use the map() function to render components dynamically based on an array of data. With this knowledge, you can now display dynamic data in your projects and create more sophisticated user interfaces for applications like booking systems, e-commerce sites, or content libraries.

React ComponentDynamic renderarray iterationNext.jsReactjs

Loading comments...

Related Examples

How to Create Scrolling Transition on Header Using Next.js 15 and Tailwind CSS
Next.JS

How to Create Scrolling Transition on Header Using Next.js 15 and Tailwind CSS

How to Set Up a Next.js 15 Application: A Step-by-Step Guide
Next.JS

How to Set Up a Next.js 15 Application: A Step-by-Step Guide

How to Deploy a Next.js Application on Vercel
Next.JS

How to Deploy a Next.js Application on Vercel

How to Highlight a Menu on Scroll of the Page Using Next.js and TailwindCSS
Next.JS

How to Highlight a Menu on Scroll of the Page Using Next.js and TailwindCSS

How to Use React Hook Form to Submit a Form in Next.js
Next.JS

How to Use React Hook Form to Submit a Form in Next.js

How to Create a Carousel in Next.js Using Slick-Carousel
Next.JS

How to Create a Carousel in Next.js Using Slick-Carousel

How to Optimize Images in Next.js - Speed Up Your Website
Next.JS

How to Optimize Images in Next.js - Speed Up Your Website

How to Create an Image Gallery using Next.js and TailwindCSS
Next.JS

How to Create an Image Gallery using Next.js and TailwindCSS