Recap: Data Fetching
Part of the Next.js Essentials section of Coddy's React journey — lesson 28 of 47.
Challenge
MediumChapter recap: a menu that loads everything on the server.
lib/data.js is provided and locked: getDishes() returns a promise for an array of { id, name, price } objects, and getDish(slug) looks a single one up.
- In
app/page.jsx, makeHomeanasyncfunction andawait getDishes()into adishesvariable. - Inside
<ul id="dish-list">, render one<li>per dish, withkey={dish.id},id={dish.id}and the dish'snameas its text. app/menu/[slug]/page.jsxshowsUnknown dishfor every URL: it destructuresparamswithout awaiting it. Awaitparamsso the lookup gets a real slug. Visiting/menu/carbonaramust showCarbonarain#dish-nameand its price in#dish-price.
Try it yourself
import './globals.css';
export const metadata = {
title: 'My App',
};
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
{children}
</body>
</html>
);
}All lessons in Next.js Essentials
2Routing & Links
Folders Are RoutesNested RoutesLinking PagesDynamic RoutesNot Found PagesRecap: Routing5Data on the Server
Async ComponentsAwaiting Route ParamsTwo Fetches at OnceLoading StatesHandling ErrorsRecap: Data Fetching