Loops in JSX
More often than not we'll be dealing with lists and collections and not just simple singular items. That means we'll need to iterate over data, but there's no way to use for
loops in JSX. Let's use .map
.
Overall, quite straightforward: there's an array of items, over which we iterate using .map
and return some JSX for each element. Similar to using components, each piece of JSX returned by the .map
transformation must have exactly one root element.
There's also the key
property on the root element of each product. This not strictly required (will work, React will just complain), but it allows React to easily distinguish between elements during the reconcilation process. Should be unique for each element in the array.
Resources
Last updated