リスト、キー、条件付きUIのレンダリング
mapで動的なコレクションをレンダリングし、適切なキーを選び、条件に応じてUIを表示・非表示にします。Reactインターフェースの基本要素を学びます。
「リスト、キー、条件付きUIのレンダリング」はCoddyKit上の無料Next.js 15 Fullstack Web Appsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはNext.js 15 Fullstack Web Apps学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Next.js 15 Fullstack Web Appsコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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
よくある質問
「リスト、キー、条件付きUIのレンダリング」レッスンは無料ですか?
はい。「リスト、キー、条件付きUIのレンダリング」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Next.js 15 Fullstack Web Appsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Next.js 15 Fullstack Web Appsコースには全4レッスンが含まれています。
「リスト、キー、条件付きUIのレンダリング」で何を学びますか?
mapで動的なコレクションをレンダリングし、適切なキーを選び、条件に応じてUIを表示・非表示にします。Reactインターフェースの基本要素を学びます。 ブラウザで直接実行するハンズオンコードでNext.js 15 Fullstack Web Appsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Next.js 15 Fullstack Web Appsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのNext.js 15 Fullstack Web Appsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「リスト、キー、条件付きUIのレンダリング」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このNext.js 15 Fullstack Web Appsレッスンでコードを書いて実行できますか?
はい。すべてのNext.js 15 Fullstack Web Appsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ReactコンポーネントとJSX
- Hooksによる状態管理
- Propsとコンポーネント間通信
- リスト、キー、条件付きUIのレンダリング