Bird
0
0

What will be the output of this Remix loader if the URL has no q parameter?

medium📝 component behavior Q5 of 15
Remix - Advanced Patterns
What will be the output of this Remix loader if the URL has no q parameter?
export async function loader({ request }) {
  const url = new URL(request.url);
  const q = url.searchParams.get('q') || '';
  const items = ['pen', 'pencil', 'paper'];
  const filtered = items.filter(item => item.includes(q));
  return json({ filtered });
}
A{"filtered": []}
B{"filtered": ["pen", "pencil", "paper"]}
C{"filtered": [""]}
DError: q is undefined
Step-by-Step Solution
Solution:
  1. Step 1: Handle missing query parameter

    If q is missing, fallback to empty string.
  2. Step 2: Filter items with empty string

    Filtering with empty string returns all items because every string includes ''.
  3. Final Answer:

    All items are returned in filtered array. -> Option B
  4. Quick Check:

    Empty query returns all items [OK]
Quick Trick: Empty query string matches all items in filter [OK]
Common Mistakes:
MISTAKES
  • Expecting empty array on missing query
  • Not providing default empty string
  • Assuming error on missing parameter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Remix Quizzes