This project demonstrates an implementation of infinite scrolling in React using the IntersectionObserver API. Infinite scrolling allows users to load more content dynamically as they scroll down the page, improving user experience by avoiding pagination.
- Efficient detection of scroll position using IntersectionObserver.
- Dynamic loading of additional items.
- Simple and customizable React components.
- Comprehensive unit tests with Vitest.
- Error handling and loading states.
- Race condition prevention using AbortController.
- React 19.2.0 - Modern React library for building user interfaces
- Vite 7.2.4 - Fast build tool and development server
- JavaScript (ES6+) - Modern JavaScript features
- ESLint 9.39.1 - Code linting and quality assurance
- Vitest - Fast unit testing framework
- React Testing Library - Testing utilities for React components
- jsdom - DOM implementation for Node.js testing environment
- IntersectionObserver API - Native browser API for detecting element visibility
- Fetch API - Modern API for making HTTP requests
- AbortController - API for canceling fetch requests
- @vitejs/plugin-react - Vite plugin for React support
- babel-plugin-react-compiler - React compiler plugin for optimization
- Clone the repository:
git clone https://github.com/gmaxsoft/InfiniteScroll.git - Navigate to the project directory:
cd InfiniteScroll - Install dependencies:
npm install
Start the development server:
npm run dev
Build the project for production:
npm run build
Preview the production build:
npm run preview
Run unit tests:
npm test
Run tests in watch mode:
npm test
Run tests with UI:
npm test -- --ui
Run tests with coverage:
npm test -- --coverage
Check code quality:
npm run lint
The project includes comprehensive unit tests covering:
-
Hook Tests (
useInfiniteScroll.test.js):- Initial state verification
- API data loading
- Error handling (network errors, invalid responses)
- Pagination logic (hasMore state)
- Request cancellation with AbortController
-
Component Tests (
PostList.test.jsx):- Component rendering
- Loading states display
- Error messages display
- Post list rendering
- End of list indicators
-
App Component Tests (
App.test.jsx):- Main container rendering
- Footer display
- Component integration
All tests are written using Vitest and React Testing Library, ensuring reliable and maintainable test coverage.
InfiniteScroll/
├── src/
│ ├── components/
│ │ ├── PostList.jsx
│ │ └── PostList.test.jsx
│ ├── hooks/
│ │ ├── useInfiniteScroll.js
│ │ └── useInfiniteScroll.test.js
│ ├── test/
│ │ └── setup.js
│ ├── App.jsx
│ ├── App.test.jsx
│ └── main.jsx
├── public/
├── package.json
├── vite.config.js
└── README.md
react^19.2.0react-dom^19.2.0
@eslint/js^9.39.1@types/react^19.2.5@types/react-dom^19.2.3@vitejs/plugin-react^5.1.1babel-plugin-react-compiler^1.0.0eslint^9.39.1eslint-plugin-react-hooks^7.0.1eslint-plugin-react-refresh^0.4.24globals^16.5.0vite^7.2.4vitest- Unit testing framework@testing-library/react- React component testing utilities@testing-library/jest-dom- Custom Jest matchers for DOM@testing-library/user-event- User interaction simulationjsdom- DOM implementation for testing
Contributions are welcome! Please fork the repository and submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.