0Pricing
Next.js 15 Fullstack Web Apps · Lezione

Rendering di liste, chiavi e UI condizionale

Esegua il rendering di raccolte dinamiche con map, scelga le chiavi corrette e mostri o nasconda la UI in modo condizionale: i fondamentali quotidiani delle interfacce React.

Rendering di liste, chiavi e UI condizionale è una lezione Next.js 15 Fullstack Web Apps gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Next.js 15 Fullstack Web Apps, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Next.js 15 Fullstack Web Apps include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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 0

Filtering 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 map to 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

Domande Frequenti

La lezione «Rendering di liste, chiavi e UI condizionale» è gratuita?

Sì — il testo completo di «Rendering di liste, chiavi e UI condizionale» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Next.js 15 Fullstack Web Apps, passa a CoddyKit PRO. Il corso Next.js 15 Fullstack Web Apps include 4 lezioni in totale.

Cosa imparerò in «Rendering di liste, chiavi e UI condizionale»?

Esegua il rendering di raccolte dinamiche con map, scelga le chiavi corrette e mostri o nasconda la UI in modo condizionale: i fondamentali quotidiani delle interfacce React. Eserciti Next.js 15 Fullstack Web Apps con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Next.js 15 Fullstack Web Apps?

Non è richiesta alcuna esperienza precedente. Next.js 15 Fullstack Web Apps su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Rendering di liste, chiavi e UI condizionale»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Next.js 15 Fullstack Web Apps?

Sì. Ogni lezione Next.js 15 Fullstack Web Apps include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Componenti React e JSX
  2. Gestione dello stato con gli Hooks
  3. Props e comunicazione tra componenti
  4. Rendering di liste, chiavi e UI condizionale
← Torna a Next.js 15 Fullstack Web Apps