Right so far?! How, then, does this become an unordered list, formatted correctly in the browser? In the .render() method, aren’t we placing an array between the <ul> tags?
Replying to my own post! Just thinking about this… So, we’re passing an array of children, and React simply renders the contents of the array, i.e. the list items? Is that what’s happening? Thinking of the {peopleLis} in the render method as simply an array of children makes more sense. I’d still appreciate any clarification with this — thanks!
The first parameter of a callback function such as .map() is always the current element being processed in the array that .map() is called on, no matter how you name it. But usually you would want to name it as something that refers to one unit of the array (a list of units) for clarity and readability reasons. In this case, the word ‘person’ seems a good fit as we could say a person is a unit of people. But the suggestion you made (using ‘people’ instead of ‘person’) would also yield the same result, as would the word potato or pretzel. Only it would make less sense to use potato or pretzel in the context.
Right so far?! How, then, does this become an unordered list, formatted correctly in the browser?
I’m new to JSX, but If I log the listItems from the lesson’s example in the browser’s console, I get this:
I used back-ticks in the .map()'s body because the console would otherwise flag it as a Syntax Error (not being able to read JSX in the browser’s console I guess). After you abstract the back-ticks away, I think this would still be the format of the returned value of .map() in JSX. So indeed an array of list tags, correctly formatted.
If we are iterating over an array and the callback function provided to the map has a single parameter, then the element we are iterating over will be provided as the argument.
If the callback function we provide to map has two parameters, then the element we are iterating over will be provided as the argument for the first parameter, while the index will be provided as the argument to the second parameter.
This is such a helpful answer and is not explained in an of the lessons. It’s important to understand this but its omision from the lessons has been very confusing.
A very good question that should’ve been explained better within the lesson… although it is mentioned in the lesson that follows. (Like mtrtmk has thoroughly described, the second parameter, the lowercase i, contains the index value of each iteration.)