Code splitting and lazy loading help your app load faster by only loading the code needed right now. This makes your app feel quicker and saves data.
Code splitting and lazy loading in Remix
import { lazy, Suspense } from 'react'; const LazyComponent = lazy(() => import('./LazyComponent')); export default function MyPage() { return ( <Suspense fallback={<div>Loading...</div>}> <LazyComponent /> </Suspense> ); }
Use React.lazy() to wrap the dynamic import of a component.
Wrap lazy components inside <Suspense> with a fallback UI shown while loading.
Profile component and shows a loading message while it loads.import { lazy, Suspense } from 'react'; const Profile = lazy(() => import('./Profile')); export default function UserPage() { return ( <Suspense fallback={<div>Loading profile...</div>}> <Profile /> </Suspense> ); }
Dashboard component to speed up the home page load.import { lazy, Suspense } from 'react'; const Dashboard = lazy(() => import('./Dashboard')); export default function Home() { return ( <Suspense fallback={<div>Loading dashboard...</div>}> <Dashboard /> </Suspense> ); }
This Remix component lazy loads HeavyComponent. While it loads, it shows "Loading heavy content...". This keeps the app fast and responsive.
import { lazy, Suspense } from 'react'; const HeavyComponent = lazy(() => import('./HeavyComponent')); export default function App() { return ( <main> <h1>Welcome to Remix App</h1> <Suspense fallback={<p>Loading heavy content...</p>}> <HeavyComponent /> </Suspense> </main> ); }
Lazy loading only works for components, not for data loading in Remix loaders.
Always provide a fallback UI inside Suspense to avoid blank screens.
Code splitting helps reduce the initial JavaScript bundle size, improving performance.
Code splitting loads only the code needed right now, making apps faster.
Use React.lazy() with Suspense to lazy load components in Remix.
Always add a fallback UI to show while the lazy component loads.
Practice
Solution
Step 1: Understand code splitting purpose
Code splitting divides the app code into smaller parts to load only what is needed immediately.Step 2: Recognize lazy loading effect
Lazy loading delays loading parts of the app until they are needed, improving speed and user experience.Final Answer:
It loads only the code needed right now, making the app faster. -> Option AQuick Check:
Code splitting = faster loading [OK]
- Thinking all code loads at once
- Confusing lazy loading with disabling JavaScript
- Believing code splitting bundles everything together
Solution
Step 1: Recall React.lazy syntax
React.lazy expects a function returning a dynamic import promise: React.lazy(() => import('...'))Step 2: Check each option
const MyComponent = React.lazy(() => import('./MyComponent')); matches correct syntax; others use invalid methods or missing React.lazy.Final Answer:
const MyComponent = React.lazy(() => import('./MyComponent')); -> Option DQuick Check:
React.lazy + dynamic import = correct lazy load [OK]
- Forgetting to wrap import in a function
- Using non-existent React.load or lazyLoad functions
- Calling import without React.lazy
import React, { Suspense } from 'react';
const LazyComp = React.lazy(() => import('./LazyComp'));
export default function App() {
return (
Loading...}>
);
}What will the user see initially when
App renders?Solution
Step 1: Understand Suspense fallback role
Suspense shows the fallback UI while the lazy component is loading.Step 2: Analyze the code behavior
Since LazyComp is lazy loaded,Loading... appears first until LazyComp finishes loading.Final Answer:
The <div>Loading...</div> text until LazyComp loads. -> Option BQuick Check:
Suspense fallback shows loading UI [OK]
- Thinking lazy components render instantly
- Forgetting Suspense fallback is required
- Assuming no UI shows during loading
const LazyComp = React.lazy(import('./LazyComp'));
export default function App() {
return ;
}Solution
Step 1: Check React.lazy usage
React.lazy expects a function returning import(), but here import() is called immediately.Step 2: Check Suspense usage
Lazy loaded components must be inside Suspense with a fallback UI to handle loading state.Final Answer:
Both B and C are errors. -> Option AQuick Check:
React.lazy needs function + Suspense fallback [OK]
- Calling import() directly inside React.lazy
- Not wrapping lazy component in Suspense
- Assuming lazy loading works without fallback
Header and Footer. You want to show a spinner while either loads. Which is the best way to implement this?Solution
Step 1: Understand Suspense scope
Suspense can wrap multiple lazy components to show one fallback UI while any are loading.Step 2: Compare options
Wrapping both in one Suspense is simpler and shows one spinner for both components loading.Final Answer:
Wrap both lazy components inside one Suspense with a single fallback spinner. -> Option CQuick Check:
One Suspense for multiple lazy components is best [OK]
- Wrapping each lazy component in separate Suspense unnecessarily
- Not using Suspense causing errors or no fallback UI
- Loading lazy components without fallback UI
Ready to prove it?
Pass the test and this pattern moves unknown
