How do you iterate N times when during each iteration you only want to use the key (index) of the iteration? There’s no array to iterate through - you could create an array like [...Array(num)]
and call map()
or forEach()
on it, but then how can you use the key in each iteration without using the item (which in this case would be undefined) and without the TypeScript compiler moaning about it:
item is declared but its value is never read
Is this a use case for the old for loop or can it be done in a simpler/safer way?
{[...Array(totalNum)].map((item: any, key: number) => {
// if I console.log(item), tsc won't moan, but clearly not a solution
return (
<SomeComponent key={key}>
</SomeComponent>
)
})}