Renderowanie list, klucze i interfejs warunkowy
Renderuj dynamiczne kolekcje za pomocą map, dobieraj poprawne klucze oraz warunkowo pokazuj i ukrywaj elementy interfejsu — to codzienne podstawy interfejsów React.
Renderowanie list, klucze i interfejs warunkowy to bezpłatna lekcja Next.js 15 Fullstack Web Apps na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Next.js 15 Fullstack Web Apps, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Next.js 15 Fullstack Web Apps zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
Rendering Collections
UIs constantly display lists: products, comments, search results. In React you turn an array of data into an array of elements, usually with Array.map.
Mapping an Array
Inside JSX you call map and return one element per item. React renders the resulting array directly.
const items = ['Apple', 'Banana', 'Cherry'];
function List() {
return (
<ul>
{items.map((fruit) => <li>{fruit}</li>)}
</ul>
);
}Why Keys Matter
React needs to track which item is which between renders. A key is a stable, unique identifier per item that lets React reuse, reorder, or remove elements efficiently instead of rebuilding everything.
Adding Keys
Pass a key prop, ideally a stable id from your data — not the array index, which breaks when items move.
{users.map((user) => (
<li key={user.id}>{user.name}</li>
))}The Index Key Pitfall
Using the array index as a key seems easy but causes bugs when the list is reordered or filtered: React reuses the wrong DOM node and component state attaches to the wrong item. Prefer a real unique id.
Conditional Rendering with &&
To render something only when a condition is true, use the logical AND operator. If the left side is falsy, nothing renders.
function Inbox({ count }) {
return (
<div>
{count > 0 && <span>You have {count} messages</span>}
</div>
);
}Ternary for Either/Or
When you must show one of two things, use a ternary expression directly in JSX.
function Status({ online }) {
return <p>{online ? 'Online' : 'Offline'}</p>;
}Early Returns
For larger branches, return early from the component. This keeps the main JSX clean.
function Profile({ user }) {
if (!user) return <p>Loading...</p>;
return <h1>{user.name}</h1>;
}Beware the Zero Trap
Using && with a number is risky: if the value is 0, React renders the literal 0 instead of nothing. Convert to a boolean first.
{messages.length > 0 && <List />} // safe
{messages.length && <List />} // BUG: renders 0Filtering and Mapping Together
You often filter then map to render a subset. Chain the array methods before returning elements.
{products
.filter((p) => p.inStock)
.map((p) => <Card key={p.id} product={p} />)}Empty States
Always handle the empty case. Show a friendly message when a list has no items rather than rendering a blank area.
{items.length === 0
? <p>No results found</p>
: items.map((i) => <Row key={i.id} {...i} />)}Quick Check
Test your list-rendering knowledge.
Recap
You learned core rendering patterns:
- Use
mapto turn data arrays into elements - Give each item a stable, unique key (not the index)
- Render conditionally with
&&, ternaries, or early returns - Watch the zero trap and always handle empty states
Często zadawane pytania
Czy lekcja „Renderowanie list, klucze i interfejs warunkowy” jest bezpłatna?
Tak — pełny tekst „Renderowanie list, klucze i interfejs warunkowy” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Next.js 15 Fullstack Web Apps, przejdź na CoddyKit PRO. Kurs Next.js 15 Fullstack Web Apps zawiera 4 lekcji w sumie.
Co nauczysz się w „Renderowanie list, klucze i interfejs warunkowy”?
Renderuj dynamiczne kolekcje za pomocą map, dobieraj poprawne klucze oraz warunkowo pokazuj i ukrywaj elementy interfejsu — to codzienne podstawy interfejsów React. Ćwiczysz Next.js 15 Fullstack Web Apps z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Next.js 15 Fullstack Web Apps?
Nie wymagamy żadnego doświadczenia. Next.js 15 Fullstack Web Apps w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Renderowanie list, klucze i interfejs warunkowy”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Next.js 15 Fullstack Web Apps?
Tak. Każda lekcja Next.js 15 Fullstack Web Apps zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Komponenty React i JSX
- Zarządzanie stanem za pomocą hooków
- Propsy i komunikacja między komponentami
- Renderowanie list, klucze i interfejs warunkowy