Performance: useLoaderData hook
MEDIUM IMPACT
This affects how data is fetched and rendered on the page, impacting initial load speed and interaction readiness.
import { useLoaderData } from '@remix-run/react'; export async function loader() { const res = await fetch('https://api.example.com/data'); return res.json(); } export default function Page() { const data = useLoaderData(); return <div>{data.title}</div>; }
function Page() {
const [data, setData] = React.useState(null);
React.useEffect(() => {
fetch('/api/data')
.then(res => res.json())
.then(json => setData(json));
}, []);
if (!data) return <div>Loading...</div>;
return <div>{data.title}</div>;
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Client-side fetch with useEffect | Initial empty DOM, then update on fetch completion | Triggers 1 reflow per data update | Higher paint cost due to loading placeholder and re-render | [X] Bad |
| Server-side fetch with useLoaderData | DOM rendered once with data | Single reflow during initial paint | Lower paint cost, stable layout | [OK] Good |