Complete the code to import the function that allows deferred data streaming in Remix.
import { [1] } from "@remix-run/node";
The defer function is used to enable deferred data streaming in Remix loaders.
Complete the loader function to return deferred data with streaming.
export async function loader() {
return [1]({
user: fetchUserData(),
posts: fetchPosts()
});
}The loader should return defer with the data to enable streaming.
Fix the error in the component to correctly use deferred data with streaming.
const data = [1]();To access deferred data in the component, use useLoaderData.
Fill both blanks to correctly render deferred data with streaming in the component.
import { Suspense } from "react"; import { Await } from "@remix-run/react"; export default function UserPosts() { const data = useLoaderData(); return ( <div> <Suspense fallback={<p>Loading user...</p>}> <[1] resolve={data.user}> {(user) => <p>{user.name}</p>} </[1]> </Suspense> <Suspense fallback={<p>Loading posts...</p>}> <[2] resolve={data.posts}> {(posts) => <ul>{posts.map(post => <li key={post.id}>{post.title}</li>)}</ul>} </[2]> </Suspense> </div> ); }
The Await component unwraps deferred promises inside Suspense boundaries.
Fill all three blanks to create a loader that defers data and a component that streams it with Suspense and Await.
import { defer } from "@remix-run/node"; export async function loader() { const userPromise = fetchUser(); const postsPromise = fetchPosts(); return [1]({ user: userPromise, posts: postsPromise }); } import { Suspense } from "react"; import { useLoaderData, [2] } from "@remix-run/react"; export default function Dashboard() { const data = useLoaderData(); return ( <div> <Suspense fallback={<p>Loading user...</p>}> <[3] resolve={data.user}> {(user) => <h1>Welcome, {user.name}</h1>} </[3]> </Suspense> </div> ); }
The loader returns defer. The component imports Await and uses it to unwrap deferred data inside Suspense.
