Overview
TheFor 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
ExtendsFor 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.
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, and0as empty conditions - When using a number, it creates an array from 0 to n-1 (like
Array(n).keys()) - The
keyprop should be provided by your render function, not on theForcomponent ForWithWrapperrenders the wrapper element even when empty unlessdisplayFallBackWhenEmptyis true- Either
childrenorrenderItemmust be provided, but not both