竈門炭治郎
水の呼吸, 日の呼吸
Forは配列をループして各アイテムに対してコンポーネントをレンダリングするために使用されるコンポーネントです。
水の呼吸, 日の呼吸
雷の呼吸
獣の呼吸
const items = useMemo(
() => [
{ name: "竈門炭治郎", breathing: ["水の呼吸", "日の呼吸"] },
{ name: "我妻善逸", breathing: ["雷の呼吸"] },
{ name: "嘴平伊之助", breathing: ["獣の呼吸"] },
],
[],
)
return (
<VStack>
<For each={items}>
{({ name, breathing }, index) => (
<Card.Root key={index}>
<Card.Header>
<Heading size="md">{name}</Heading>
</Card.Header>
<Card.Body pt="xs">
<Text fontSize="sm" color="fg.muted">
{breathing.join(", ")}
</Text>
</Card.Body>
</Card.Root>
)}
</For>
</VStack>
)
import { For } from "@yamada-ui/react"
import { For } from "@/components/ui"
import { For } from "@workspaces/ui"
<For />
配列が空または未定義の場合にフォールバックコンポーネントをレンダリングする場合は、fallbackプロパティを使用します。
There are no items to show
<VStack>
<For
each={[]}
fallback={
<EmptyState.Root
description="There are no items to show"
indicator={<BoxIcon />}
/>
}
>
{(item, index) => <Text key={index}>{item}</Text>}
</For>
</VStack>
フィルタリングする場合は、filterプロパティを使用します。
雷の呼吸
獣の呼吸
const items = useMemo(
() => [
{ name: "竈門炭治郎", breathing: ["水の呼吸", "日の呼吸"] },
{ name: "我妻善逸", breathing: ["雷の呼吸"] },
{ name: "嘴平伊之助", breathing: ["獣の呼吸"] },
],
[],
)
return (
<VStack>
<For each={items} filter={({ breathing }) => breathing.length == 1}>
{({ name, breathing }, index) => (
<Card.Root key={index}>
<Card.Header>
<Heading size="md">{name}</Heading>
</Card.Header>
<Card.Body pt="xs">
<Text fontSize="sm" color="fg.muted">
{breathing.join(", ")}
</Text>
</Card.Body>
</Card.Root>
)}
</For>
</VStack>
)
ソートする場合は、sortプロパティを使用します。
雷の呼吸
獣の呼吸
水の呼吸, 日の呼吸
const items = useMemo(
() => [
{ name: "竈門炭治郎", breathing: ["水の呼吸", "日の呼吸"] },
{ name: "我妻善逸", breathing: ["雷の呼吸"] },
{ name: "嘴平伊之助", breathing: ["獣の呼吸"] },
],
[],
)
return (
<VStack>
<For each={items} sort={(a, b) => a.breathing.length - b.breathing.length}>
{({ name, breathing }, index) => (
<Card.Root key={index}>
<Card.Header>
<Heading size="md">{name}</Heading>
</Card.Header>
<Card.Body pt="xs">
<Text fontSize="sm" color="fg.muted">
{breathing.join(", ")}
</Text>
</Card.Body>
</Card.Root>
)}
</For>
</VStack>
)
配列の途中の要素から開始する場合は、offsetに開始位置を設定します。
雷の呼吸
獣の呼吸
const items = useMemo(
() => [
{ name: "竈門炭治郎", breathing: ["水の呼吸", "日の呼吸"] },
{ name: "我妻善逸", breathing: ["雷の呼吸"] },
{ name: "嘴平伊之助", breathing: ["獣の呼吸"] },
],
[],
)
return (
<VStack>
<For each={items} offset={1}>
{({ name, breathing }, index) => (
<Card.Root key={index}>
<Card.Header>
<Heading size="md">{name}</Heading>
</Card.Header>
<Card.Body pt="xs">
<Text fontSize="sm" color="fg.muted">
{breathing.join(", ")}
</Text>
</Card.Body>
</Card.Root>
)}
</For>
</VStack>
)
表示件数を制限する場合は、limitを設定します。
水の呼吸, 日の呼吸
雷の呼吸
const items = useMemo(
() => [
{ name: "竈門炭治郎", breathing: ["水の呼吸", "日の呼吸"] },
{ name: "我妻善逸", breathing: ["雷の呼吸"] },
{ name: "嘴平伊之助", breathing: ["獣の呼吸"] },
],
[],
)
return (
<VStack>
<For each={items} limit={2}>
{({ name, breathing }, index) => (
<Card.Root key={index}>
<Card.Header>
<Heading size="md">{name}</Heading>
</Card.Header>
<Card.Body pt="xs">
<Text fontSize="sm" color="fg.muted">
{breathing.join(", ")}
</Text>
</Card.Body>
</Card.Root>
)}
</For>
</VStack>
)
並び順を反転させる場合は、reverseをtrueに設定します。
獣の呼吸
雷の呼吸
水の呼吸, 日の呼吸
const items = useMemo(
() => [
{ name: "竈門炭治郎", breathing: ["水の呼吸", "日の呼吸"] },
{ name: "我妻善逸", breathing: ["雷の呼吸"] },
{ name: "嘴平伊之助", breathing: ["獣の呼吸"] },
],
[],
)
return (
<VStack>
<For each={items} reverse>
{({ name, breathing }, index) => (
<Card.Root key={index}>
<Card.Header>
<Heading size="md">{name}</Heading>
</Card.Header>
<Card.Body pt="xs">
<Text fontSize="sm" color="fg.muted">
{breathing.join(", ")}
</Text>
</Card.Body>
</Card.Root>
)}
</For>
</VStack>
)
| Prop | デフォルト | 型 / 説明 |
|---|---|---|
children | - | (value: Y, index: number, array: Y[]) => ReactNodeThe render function to render each item in the array. |
each | - | readonly Y[] | undefined | Y[]The array to iterate over. |
fallback | - | ReactNodeThe fallback content to render when the array is empty. |
filter | - | (value: Y, index: number, array: Y[]) => booleanA function that returns a boolean indicating whether the item should be included in the render result. |
limit | - | numberThe maximum number of items to include in the render result. |
offset | 0 | numberThe number of items to skip before including them in the render result. |
reverse | false | booleanThe boolean value to determine the order of the items in the array.
If |
sort | - | (a: Y, b: Y) => numberThe function to sort the items in the array.
If function is provided, the items will be sorted based on the return value.
If |