Course Content
Next.js 14 Mastery for Building Modern Web Apps
Next.js 14 Mastery for Building Modern Web Apps
1. Introduction to Next.js
2. Setting Up a Next.js Project
Setting Up the Next.js Project Understanding the Project File and Folder StructureStyling Approaches in Next.jsAdding the Global CSS FileUsing Tailwind CSS for StylingWorking with CSS ModulesApplying Styles ConditionallyAdding Custom Google FontsChallenge: Implementing a Custom Google FontHandling Images in Next.js
3. Building Pages and Layouts in Next.js
4. Deploying a Next.js App and Setting Up a Database
5. Fetching and Displaying Data in Next.js
6. Advanced Next.js Features and Optimizations
7. Implementing Authentication in Next.js
Understanding AuthenticationSetting Up the Login RouteUsing NextAuth.js for AuthenticationConfiguring Authentication and Protecting RoutesHashing Passwords and Managing CredentialsImplementing Sign-In FunctionalityConnecting the UI with Authentication LogicImplementing Logout FunctionalityFinal Thoughts and Next Steps
Adding Pagination to a Next.js App
To enable users to navigate through different pages and view all invoices, implement pagination using URL parameters, similar to the search functionality.
Back to the Project
Step 1
- If you navigate to the
Pagination
component, you see that it is a client component. To avoid exposing database secrets, refrain from fetching data on the client side. Instead, fetch the data on the server and pass it as a prop to the component; - In
app/dashboard/invoices/page.tsx
, import a new function calledfetchInvoicesPages
and pass the search query fromsearchParams
as an argument:fetchInvoicesPages
calculates the total number of pages based on the search query. For instance, if there are 12 matching invoices with a display of 6 per page, the total number of pages would be 2.
- Pass the
totalPages
prop to the<Pagination/>
component.
Step 2
- Navigate to the
app/ui/invoices/pagination.tsx
; - Import the
usePathname
anduseSearchParams
hooks; - Use these hooks to retrieve the current page and set the new page;
- Uncomment the code in this component.
Note
Your application may temporarily break as the
<Pagination/>
logic is not yet implemented. We will work on it later.
Step 3
- Inside the
<Pagination>
component, create a new function calledcreatePageURL
; - Similar to the search functionality, utilize
URLSearchParams
to set the new page number; - Use
pathName
to construct the URL string; - Uncomment the
allPages
variable.
Step 4
Final step - when the user inputs a new search query, it's essential to reset the page number to 1. To achieve this, update the handleSearch
function in the app/ui/search.tsx
.
In Practice
Everything was clear?
Thanks for your feedback!
Section 6. Chapter 3