Problem Statement
Show images from the Picsum API five at a time, with page buttons under the grid to move between pages.
Functional Requirements
- Fetch per page: Load
https://picsum.photos/v2/list?page=<n>&limit=5whenever the page number changes, starting at page 1. - Page numbers: Show up to three pages before the current page and the current page plus the next three.
- Active page: Highlight the current page button with the
activeclass. - Previous and next: The
<button shows only when the page is above 1. The>button is always shown. - Jump to page: Clicking a number loads that page.
Technical Constraints
- Keep
pageNoin the parentPostcomponent and passsetPageNodown toPagination. - Ignore a response that arrives after the page has already changed, so a slow request cannot overwrite newer data.
Code Flow Overview
-
Initial Load
- Post component mounts
- Initial state setup with empty data array and pageNo = 1
- useEffect triggers initial API call
- Images render and pagination controls display
-
Page Navigation
- User clicks pagination button
- pageNo state updates
- useEffect triggers with new pageNo
- New images fetch and display
- Pagination numbers update based on new pageNo
Function Explanations
Post Component Functions:
- fetchData
- Async function that calls Picsum API
- Takes current page number as parameter
- Updates data state with new images
- Handles API response with axios
Pagination Component Functions:
-
prevThreeNo Generation
- Creates array of three previous page numbers
- Filters out negative numbers
- Example: If current page is 5, generates [2,3,4]
-
nextFourNo Generation
- Creates array of next four page numbers
- Example: If current page is 5, generates [5,6,7,8]
-
handleNext
- Increments page number by 1
- Triggers new data fetch
-
handlePrev
- Decrements page number by 1
- Only active when page > 1
- Triggers new data fetch
Key Concepts Explained
Array.from()
-
Basic Syntax
Array.from(arrayLike, mapFunction) -
Common Use Cases
- Converting array-like objects to arrays
- Creating numeric sequences
- Generating arrays with computed values
-
Examples:
// Generate 1 to 5 Array.from({length: 5}, (_, i) => i + 1) // Result: [1,2,3,4,5] // Generate multiples of 3 Array.from({length: 4}, (_, i) => i * 3) // Result: [0,3,6,9] // Generate custom sequence Array.from({length: 3}, (_, i) => `Item ${i+1}`) // Result: ['Item 1', 'Item 2', 'Item 3']
State Management Concepts:
-
useState Hook
- Manages component local state
- Triggers re-renders on updates
- Maintains state between renders
-
useEffect Hook
- Handles side effects (API calls)
- Dependency array controls when effect runs
- Cleans up previous effects
Array Methods Used:
-
map()
- Transforms array elements
- Creates new array
- Used for rendering lists
-
filter()
- Removes unwanted elements
- Creates new filtered array
- Used in pagination logic
-
reverse()
- Reverses array order
- Used for page number display
Props Pattern:
-
State Lifting
- pageNo managed in parent
- Passed down via props
- Child components update via callbacks
-
Controlled Components
- Page navigation controlled by parent
- State changes trigger re-renders
- Maintains single source of truth
API Integration:
-
Axios Usage
- Promise-based HTTP client
- Handles API requests
- Manages response data
-
Dynamic URL Construction
- URL changes with page number
- Limit parameter controls items per page
- Response handling with async/await