Repository navigation
For
Similar to using .map() but customizable using slots, can also loop x number of times
- Target index for alternative render using slots
- Use negative indexes to target end of array
- Access item in array as slot function argument
- Makes loops easier
- Nested mapping is easier
Use this component when your trying to use .map() but want to target different indexes for alternative render, or you want to loop x number of times
<For {...['one', 'two', 'three']}>
{i => <li>{i}</li>}
</For><li>one</li>
<li>two</li>
<li>three</li>This component expects an array to be spread over its props like:
<For {...[1, 2, 3]}>Note: You cannot use the
loopprop when using spread syntax<For {...[]}>
Loop x number of times, returns current index as slot argument
Note: You cannot use the spread syntax
<For {...[]}>when using theloopprop
Access current value in array using slot function arguments
interface Arguments {
value: any;
index: number;
array: any[];
}<For {...['one', 'two', 'three']}>
{(value, index, array) => ...}
</For>Default/fallback render if no slot is defined
Target an index in passed array using a number as a named slot
Use negative numbers to target the end of the array (-1 targets last item in array)
<For {...['one', 'two', 'three']}>
{i => <li>{i}</li>}
</For><li>one</li>
<li>two</li>
<li>three</li>Using loop prop
<For loop={3}>
{i => <li>{i}</li>}
</For><li>0</li>
<li>1</li>
<li>2</li><For {...['one', 'two', 'three']}>
{i => <li>{i}</li>}
<second slot="1">
{i => <li>{i} (Second)</li>}
</second>
</For><li>one</li>
<li>two (Second)</li>
<li>three</li><For {...['one', 'two', 'three']}>
{i => <li>{i}</li>}
<last slot="-1">
{i => <li>{i} (last)</li>}
</last>
</For><li>one</li>
<li>two</li>
<li>three (last)</li><For {...['one', 'two', 'three']}>
{i => <li>
{i}
<ul>
<For {...i}>
{i => <li>{i}</li>}
</For>
</ul>
</li>}
</For>