Challenge - 5 Problems
Remix Loader Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Remix loader function return?
Consider this loader function in a Remix route. What will be the value of
data inside the component?Remix
import { json } from '@remix-run/node'; import { useLoaderData } from '@remix-run/react'; export async function loader() { return json({ message: 'Hello from loader!' }); } export default function Component() { const data = useLoaderData(); return <div>{data.message}</div>; }
Attempts:
2 left
💡 Hint
Remember that loader returns data accessible via useLoaderData hook.
✗ Incorrect
The loader returns a JSON response with a message. useLoaderData reads this data, so the component renders the message string.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this Remix loader function
Which option correctly fixes the syntax error in this loader function?
Remix
export async function loader() { const data = await fetch('https://api.example.com/data') return data.json() }
Attempts:
2 left
💡 Hint
Remember that data.json() returns a promise and needs to be awaited.
✗ Incorrect
data.json() returns a promise, so to get the parsed JSON, you must await it before returning.
❓ state_output
advanced2:00remaining
What is the output when loader throws an error?
Given this loader function that throws an error, what will the user see by default?
Remix
export async function loader() { throw new Response('Not Found', { status: 404 }); } import { useLoaderData } from '@remix-run/react'; export default function Component() { const data = useLoaderData(); return <div>{data}</div>; }
Attempts:
2 left
💡 Hint
Throwing a Response with status 404 triggers Remix's error handling.
✗ Incorrect
Throwing a Response with status 404 causes Remix to show its default 404 error page instead of rendering the component.
🧠 Conceptual
advanced2:00remaining
Why use loader functions instead of fetching data inside components?
Which reason best explains why Remix encourages data fetching in loader functions rather than inside React components?
Attempts:
2 left
💡 Hint
Think about when loader functions run compared to component rendering.
✗ Incorrect
Loader functions run on the server before the page renders, so data is ready immediately. This improves performance and SEO.
🔧 Debug
expert3:00remaining
Why does this loader function cause a runtime error?
This loader function is intended to fetch JSON data and return it. Why does it cause a runtime error?
Remix
export async function loader() { const response = await fetch('https://api.example.com/data'); if (!response.ok) { throw new Error('Failed to fetch'); } return response.json; }
Attempts:
2 left
💡 Hint
Check how response.json is used in the return statement.
✗ Incorrect
The code returns response.json (a function) instead of calling it with parentheses. This causes the component to receive a function, not data, leading to errors.