In React.js documentation, I wonder how Array.map()
is used in JSX React.
JavaScript
x
12
12
1
function NumberList(props) {
2
const numbers = props.numbers;
3
const listItems = numbers.map((number) =>
4
<li key={number.toString()}>
5
{number}
6
</li>
7
);
8
return (
9
<ul>{listItems}</ul>
10
);
11
}
12
in <ul>
tag, why we just put the variable listItems
directly?. Because I think it will return a single array instead of <li>
elements like this :
JavaScript
1
8
1
<ul>
2
[
3
<li></li>,
4
<li></li>,
5
<li></li>,
6
]
7
</ul>
8
how does JSX treat an array?
Did I have to loop listItems
manually?
Thank you in advance.
Advertisement
Answer
you might want to take a look here: https://stackabuse.com/how-to-loop-in-react-jsx/ . I hope this is what you are looking for