This project implements an infinite horizontal scrolling feature using Next.js. Data is fetched from the Rick and Morty API and displayed as a sequence of character cards. When the user reaches the end of the list, more data is automatically loaded, ensuring a smooth infinite scrolling experience.
Demo.video.mov
- 📜 Infinite Horizontal Scroll: Scroll horizontally to view cards, and once you reach the end, new cards are fetched automatically.
- 🔄 Automatic API Calls: Data is fetched dynamically using
fetch
when scrolling reaches the end. - 📌 Intersection Observer: Efficiently detects when to fetch more data.
- 🚀 Next.js & React Hooks: Optimized for performance and smooth rendering.
- 🛠️ Minimal UI: Clean and responsive card-based layout.
- Frontend: Next.js, React, TypeScript
- Data Fetching: Native
fetch
API - State Management:
useState
anduseEffect
- Infinite Scroll Handling: Intersection Observer API
- Styling: Tailwind CSS
git clone https://github.com/your-repo/infinite-scroll-nextjs.git
cd infinite-scroll-nextjs
npm install # or yarn install
npm run dev # or yarn dev
Now, open http://localhost:3000 in your browser.
/infinite-scroll-nextjs
│── /components
│ ├── Card.tsx # UI component for displaying a character
│── /lib
│ ├── api.ts # API fetching logic
│── /pages
│ ├── index.tsx # Main page with infinite scroll logic
│── public # Static assets
│── styles # CSS styles (if any)
│── README.md # Project documentation
│── package.json # Dependencies and scripts
The project fetches data from the Rick and Morty API:
- Base URL:
https://rickandmortyapi.com/api/character
- Pagination: The API supports pagination using the
page
parameter. - Example API Call:
https://rickandmortyapi.com/api/character?page=1
- Initial Fetch: On page load, the first set of characters is fetched from the API.
- Scrolling Detection: Using the
IntersectionObserver
, the app detects when the user reaches the end of the scroll. - Fetching More Data: Once triggered, it fetches the next page of characters and appends them to the existing list.
- Repeat: This continues indefinitely, ensuring an endless scrolling experience.
- 🔹 Skeleton Loaders for a better UX while fetching data.
- 🔹 Error Handling & Retry Logic for API failures.
- 🔹 Lazy Loading Images to optimize performance.
- 🔹 Caching API Responses to reduce redundant requests.
👨💻 Utkarsh Alshi
📧 [email protected]
🔗 Github - https://github.com/UtkarshAlshi
🔗 Linkedin - https://www.linkedin.com/in/utkarsh-alshi-70b0b6227/
Made with ❤️ using Next.js & React.