useLoaderData. What will it display on the page?import { useLoaderData } from '@remix-run/react'; export async function loader() { return { message: 'Hello from loader!' }; } export default function Greeting() { const data = useLoaderData(); return <div>{data.message}</div>; }
The loader function returns an object with a message property. The useLoaderData hook retrieves this object, so data.message is 'Hello from loader!'. The component renders this string inside a <div>.
data inside the component?data inside the component?import { useLoaderData } from '@remix-run/react'; export async function loader() { return { user: { id: 1, name: 'Alice' }, loggedIn: true }; } export default function UserProfile() { const data = useLoaderData(); return <div>{data.user.name}</div>; }
The loader returns an object with two properties: user and loggedIn. The useLoaderData hook returns exactly what the loader returns, so data is the full object.
useLoaderData to get a list of items?useLoaderData to access an array of items returned by the loader.export async function loader() { return { items: ['apple', 'banana', 'cherry'] }; } export default function ItemList() { // Fill in the blank const data = _____; return ( <ul> {data.items.map(item => <li key={item}>{item}</li>)} </ul> ); }
useLoaderData is a hook that must be called as a function to get the loader data. Option A correctly calls it as useLoaderData(). Option A tries to access a property on the function itself, which is invalid. Option A is just the function reference, not called. Option A calls the loader function directly, which is not how hooks work.
import { useLoaderData } from '@remix-run/react'; export default function Broken() { const data = useLoaderData(); return <div>{data.message}</div>; }
The component uses useLoaderData but no loader function is defined for this route. Without a loader, useLoaderData returns undefined, so accessing data.message throws an error.
useLoaderData outside a Remix route component?useLoaderData inside a non-route component or outside the React tree. What is the expected behavior?useLoaderData relies on Remix's routing context. If called outside a route component, it throws an error because the context is missing.