Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
6 views
4 pages
Pagination
This is a simple explanation of how to do pagination in nodejs
Uploaded by
ِIbnabbas
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save pagination For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
6 views
4 pages
Pagination
This is a simple explanation of how to do pagination in nodejs
Uploaded by
ِIbnabbas
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save pagination For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save pagination For Later
You are on page 1
/ 4
Search
Fullscreen
+ Get unlimited access to the best of Medium for less than $1/week. Become amember x How to implement scroll pagination in MERN application A Usama Sattar - Follow Y 3mintead - Nov 10,2023 Se Q n © GPagination plays a vital role in optimizing user experience by breaking down extensive data into manageable portions. In the realm of MERN (MongoDB, Express, React, Node.js) applications, the integration of pagination enhances the efficiency of handling large datasets, such as blog posts, products, or user-generated content. This comprehensive guide will explore the seamless implementation of pagination within a MERN stack, using front-end React components that interact with back-end Express APIs. We'll use MongoDB’s powerful querying capabilities, alongside Node.js for server-side logic, to create a smooth pagination system. By the end, you'll have a solid understanding of how to structure and implement pagination. Backend (Node js and Mongo db) To enable pagination in a MERN application, the first step is defining an Express route to fetch paginated user data. This route triggers a controller method responsible for interacting with the database and retrieving the specific subset of users. router.get("/users", getAllUsers) ; Now, create controller function to fetch paginated users. The controller function receives page from query, calls service function and sends resposne. //controUler static asyne getAllsers(req, res) { let page = req.query.page //starts from 0let users= await UserService.getUsersPaginated (page) ‘if (users && users.length > 6) { res. json(users) } else { res.json("users not found") } The service function uses limit, sort, and skip functions of mongo db to facilitates the extraction of a specified subset of data. /{service const getUsersPaginated = async (page) => { let resultsPerPage = 10 return await UserModel. Find ({}) ssort({ createdAt: ‘descending’ }) sTean() - Limit (resultsPerPage) -skip(page * resultsPerPage) Now, our backend is ready, we need to implement frontend using React. Frontend (React js) We will create states to deal with users and page number. Below code gets initial data inside useEffect hook. Whenever user changes page number, the index state triggers useEffect to fetch more data and append it the previous users state, const [users, setUsers] = useState([]); const [index, setIndex] = useState(9) ;useEffect(() => { const getUsers= async (index) => { const response = await axios.get(” /users?page=0") 5 setNot’ fications (response. data) ; % getUsers(a); +, 15 useEffect(() => { if Gindex t== 0) { appendData() ; Openinapp 7 Q Search Awe O | const temp = [...users]; const loadedbata = response.data; const appended = temp.concat (loadedData) ; setUsers appended) ; a const fetchData = async () => { setIndex((prev) => prev + 1); oH In this example we are using scroll pagination, so we need to install react- infinite-scroll-component library using npm or yarn. npn i react~infinite-scroll-component Now everything is ready we need to make component UI.
You might also like
Juntao Qiu - Advanced Data Fetching Patterns in React - 2024
PDF
No ratings yet
Juntao Qiu - Advanced Data Fetching Patterns in React - 2024
105 pages
MERN Stack CRUD Operations
PDF
100% (2)
MERN Stack CRUD Operations
28 pages
MERN Stack Full Stack MCQs
PDF
No ratings yet
MERN Stack Full Stack MCQs
2 pages
Next Level Nextjs
PDF
No ratings yet
Next Level Nextjs
45 pages
FSD - Unit - 5
PDF
No ratings yet
FSD - Unit - 5
44 pages
The Road To Graphql
PDF
100% (4)
The Road To Graphql
356 pages
Step-by-Step Tutorial - Building A MERN Stack Application From Scratch
PDF
No ratings yet
Step-by-Step Tutorial - Building A MERN Stack Application From Scratch
14 pages
Getting Started With The MERN Stack
PDF
100% (1)
Getting Started With The MERN Stack
11 pages
Introduction To MERN Stack
PDF
No ratings yet
Introduction To MERN Stack
10 pages
Todo List Application Using MERN
PDF
No ratings yet
Todo List Application Using MERN
9 pages
Mern Stack
PDF
No ratings yet
Mern Stack
17 pages
Top React Coding Interview Questions
PDF
No ratings yet
Top React Coding Interview Questions
12 pages
INT252 CAreport
PDF
100% (1)
INT252 CAreport
17 pages
How To Implement Infinite Scroll in Next - Js With Intersection Observer
PDF
No ratings yet
How To Implement Infinite Scroll in Next - Js With Intersection Observer
9 pages
Pagination in Golang and MongoDB - DEV Community
PDF
No ratings yet
Pagination in Golang and MongoDB - DEV Community
7 pages
React
PDF
No ratings yet
React
12 pages
MERN Progress
PDF
No ratings yet
MERN Progress
11 pages
Programs
PDF
No ratings yet
Programs
7 pages
WAD - Lab 3 B
PDF
No ratings yet
WAD - Lab 3 B
10 pages
Resume Builder
PDF
No ratings yet
Resume Builder
35 pages
Shub AP1
PDF
No ratings yet
Shub AP1
6 pages
2 Marks-2
PDF
No ratings yet
2 Marks-2
10 pages
Chat App Code Project
PDF
No ratings yet
Chat App Code Project
67 pages
React Pagination Component - Material UI
PDF
No ratings yet
React Pagination Component - Material UI
6 pages
Unit IV Notes
PDF
No ratings yet
Unit IV Notes
10 pages
FSD Material
PDF
No ratings yet
FSD Material
122 pages
GraphQL API Gateway
PDF
No ratings yet
GraphQL API Gateway
16 pages
React Exp 004
PDF
No ratings yet
React Exp 004
4 pages
Program 16
PDF
No ratings yet
Program 16
8 pages
Sadsjryjtc74tv8
PDF
No ratings yet
Sadsjryjtc74tv8
3 pages
Simple Todo List Application
PDF
No ratings yet
Simple Todo List Application
6 pages
MERN Stack Interview QA
PDF
No ratings yet
MERN Stack Interview QA
6 pages
Ex 2 TODO AppN
PDF
No ratings yet
Ex 2 TODO AppN
11 pages
React Exemple
PDF
No ratings yet
React Exemple
1 page
This Won'T Trigger A Re-Render:: Updatecheckbox (Checked) (
PDF
No ratings yet
This Won'T Trigger A Re-Render:: Updatecheckbox (Checked) (
29 pages
Assignment 3b
PDF
No ratings yet
Assignment 3b
6 pages
React React Is A JavaScript Library
PDF
No ratings yet
React React Is A JavaScript Library
2 pages
React Lab Manual
PDF
No ratings yet
React Lab Manual
30 pages
Module 67 Web Development
PDF
No ratings yet
Module 67 Web Development
13 pages
Mern Crud Guide
PDF
No ratings yet
Mern Crud Guide
4 pages
MERN Simple Project
PDF
No ratings yet
MERN Simple Project
6 pages
React JS01
PDF
No ratings yet
React JS01
19 pages
React 3
PDF
No ratings yet
React 3
4 pages
Programs
PDF
No ratings yet
Programs
14 pages
NOTES 5.2 (React Todo)
PDF
No ratings yet
NOTES 5.2 (React Todo)
3 pages
Exercise-8 MST Programs
PDF
No ratings yet
Exercise-8 MST Programs
22 pages
Sunidhi AP Exp 1 - Removed-1
PDF
No ratings yet
Sunidhi AP Exp 1 - Removed-1
5 pages
Mern Stack
PDF
No ratings yet
Mern Stack
4 pages
MERN Stack QA Compiled
PDF
No ratings yet
MERN Stack QA Compiled
7 pages
ASSESSMENT PART 3 (Updated) - Andrew Baldonado
PDF
No ratings yet
ASSESSMENT PART 3 (Updated) - Andrew Baldonado
13 pages
FSD Unit 5
PDF
No ratings yet
FSD Unit 5
21 pages
Advance Web Assignment 1 To 8
PDF
No ratings yet
Advance Web Assignment 1 To 8
7 pages
The Power of GraphQL
PDF
No ratings yet
The Power of GraphQL
52 pages
Building Full-Stack Applications With MERN Stack - Comprehensive Guide
PDF
No ratings yet
Building Full-Stack Applications With MERN Stack - Comprehensive Guide
13 pages
Web Design and Development PR
PDF
No ratings yet
Web Design and Development PR
18 pages
Okay
PDF
No ratings yet
Okay
22 pages
MERN Todo Project
PDF
No ratings yet
MERN Todo Project
3 pages