Skip to main content

Overview

The For component provides a declarative way to render lists in React with full TypeScript support. It handles empty states, supports both arrays and numeric ranges, and offers a wrapper variant for semantic HTML.

Import

For

Props

readonly unknown[] | number
required
The array to iterate over, or a number to create a range from 0 to n-1.
(item: TItem, index: number, array: TItem[]) => React.ReactNode
Render function called for each item. Receives the item, index, and the full array.
(item: TItem, index: number, array: TItem[]) => React.ReactNode
Alternative to children. Same signature and behavior.
React.ReactNode
Content to render when the array is empty or the number is 0.

ForWithWrapper

Extends For with a wrapper element for semantic HTML lists.

Additional Props

React.ElementType
default:"ul"
The wrapper element type to render.
boolean
default:"false"
When true, shows the fallback instead of an empty wrapper when the list is empty.
All other props from the wrapper element (like className, style, etc.) are also supported.

Type Definitions

Usage Examples

Basic List Rendering

With Fallback

Using renderItem Prop

Numeric Range

With Wrapper Element

Custom Wrapper

Accessing Index and Array

Table Rows

Notes

  • The component treats null, undefined, empty arrays, and 0 as empty conditions
  • When using a number, it creates an array from 0 to n-1 (like Array(n).keys())
  • The key prop should be provided by your render function, not on the For component
  • ForWithWrapper renders the wrapper element even when empty unless displayFallBackWhenEmpty is true
  • Either children or renderItem must be provided, but not both