📖 Scenario: You are building a simple web app using Remix Framework that shows a list of books from a database. The database has many books, but you want to show only the books published after the year 2000 to keep the page fast and clean.
🎯 Goal: Build a Remix loader function that fetches only books published after 2000 from the database, and display their titles on the page.
📋 What You'll Learn
Create a mock database array called books with book objects containing id, title, and year.
Add a variable called yearThreshold set to 2000 to filter books.
Use a filter method to select books published after yearThreshold.
Complete the Remix loader function to return the filtered books and display their titles in the component.
💡 Why This Matters
🌍 Real World
Filtering data before sending it to the client helps keep web pages fast and responsive, especially when working with large databases.
💼 Career
Understanding how to optimize database queries and use Remix loaders is important for building efficient full-stack web applications.
Progress0 / 4 steps
1
Create the initial book data
Create a constant array called books with these exact objects: { id: 1, title: 'The Hobbit', year: 1937 }, { id: 2, title: 'Harry Potter', year: 1997 }, { id: 3, title: 'The Da Vinci Code', year: 2003 }, { id: 4, title: 'The Martian', year: 2011 }.
Remix
Hint
Use const books = [ ... ] with the exact book objects inside.
2
Add the year threshold variable
Add a constant called yearThreshold and set it to 2000.
Remix
Hint
Use const yearThreshold = 2000; exactly.
3
Filter books published after the threshold
Create a constant called filteredBooks that uses books.filter to keep only books where book.year > yearThreshold.
Remix
Hint
Use books.filter(book => book.year > yearThreshold) to create filteredBooks.
4
Complete the Remix loader and display titles
Write a Remix loader function that returns filteredBooks as JSON. Then create a default export React component that uses useLoaderData to get the books and renders an unordered list <ul> with each book's title inside a <li>.
Remix
Hint
Use export const loader = () => json(filteredBooks); and a React component that calls useLoaderData to get the books and render them in a list.
Practice
(1/5)
1. What is the main benefit of optimizing database queries in a Remix app?
easy
A. It makes the UI look better
B. It makes the app faster and reduces server load
C. It increases the size of the database
D. It adds more data to the database
Solution
Step 1: Understand query optimization purpose
Optimizing queries means making them run faster and use fewer resources.
Step 2: Connect optimization to app performance
Faster queries reduce server load and improve user experience by loading data quickly.
Final Answer:
It makes the app faster and reduces server load -> Option B
Quick Check:
Optimization = faster app and less server load [OK]
Hint: Optimization means faster queries and less server work [OK]
Common Mistakes:
Thinking optimization adds more data
Confusing UI appearance with query speed
Believing optimization increases database size
2. Which of the following is the correct way to limit results in a Remix loader using Prisma?
easy
A. const users = await prisma.user.findMany({ max: 10 });
B. const users = await prisma.user.findMany({ limit: 10 });
C. const users = await prisma.user.findMany({ take: 10 });
D. const users = await prisma.user.findMany({ count: 10 });
Solution
Step 1: Recall Prisma syntax for limiting results
Prisma uses the 'take' option to limit the number of records returned.
Step 2: Identify correct option
Only const users = await prisma.user.findMany({ take: 10 }); uses 'take: 10', which is the correct syntax.
A. The 'limit' option is invalid in Prisma queries
B. The 'gt' operator is not supported
C. Missing 'orderBy' clause causes error
D. The 'where' clause syntax is incorrect
Solution
Step 1: Check Prisma query options
Prisma uses 'take' to limit results, not 'limit'. Using 'limit' causes an error.
Step 2: Confirm other parts are correct
'gt' is valid for greater than, and 'where' syntax is correct.
Final Answer:
The 'limit' option is invalid in Prisma queries -> Option A
Quick Check:
Use 'take' not 'limit' in Prisma [OK]
Hint: Replace 'limit' with 'take' in Prisma queries [OK]
Common Mistakes:
Using 'limit' instead of 'take'
Thinking 'gt' is invalid
Assuming missing 'orderBy' causes error
5. You want to optimize a Remix loader that fetches posts with their authors but only needs the post title and author name. Which Prisma query is best for performance?