I’m just learning react and I saw so far 2 ways of rendering lists, the first declaring a const after mapping the data and then rendering this new list and the second one mapping directly inside the jsx
Which way would be more appropiate, is used normally?
We’re bordeline on style/opinion here, but my take on this is that:
option #2 is better when it’s really as simple as what you wrote (directly mapping a list of instances into a list of components without logic in between). And it does it all in one place, so readability is best.
I don’t think #1 is a good option for two reasons: the JSX stuff should be (as much as possible) in the render part (i.e. the return) not before the render. Above the render, we manipulate the data, after we build the DOM. And also it makes you look at two places for something trivial that should be done all at once.
And if you ever need to make the map lambda more complex than a direct one liner, then don’t. Either transform the data before the render, or do it in the <Card /> component.
N.B.: I’m giving the opposite answer from @gaitat, but with a rationale: [before return it’s data, after it’s JSX]. And I agree with the principle, it’s just that I consider a .map() not being code complexity.
I agree with zmo, that the 2nd option is better. Keeping the stuff you are going to render together improves readability and cognitive load.
But there is a 3rd option; creating an intermediary component i.e <CardContainer> that renders a list of cards. This way the <App> is off the hook when is comes to rendering the <Card> components.
Also, when you use map, don’t forget to assign a unique key to your items!
2
Answers
We’re bordeline on style/opinion here, but my take on this is that:
option #2 is better when it’s really as simple as what you wrote (directly mapping a list of instances into a list of components without logic in between). And it does it all in one place, so readability is best.
I don’t think #1 is a good option for two reasons: the JSX stuff should be (as much as possible) in the render part (i.e. the return) not before the render. Above the render, we manipulate the data, after we build the DOM. And also it makes you look at two places for something trivial that should be done all at once.
And if you ever need to make the map lambda more complex than a direct one liner, then don’t. Either transform the data before the render, or do it in the
<Card />
component.N.B.: I’m giving the opposite answer from @gaitat, but with a rationale: [before return it’s data, after it’s JSX]. And I agree with the principle, it’s just that I consider a
.map()
not being code complexity.I agree with zmo, that the 2nd option is better. Keeping the stuff you are going to render together improves readability and cognitive load.
But there is a 3rd option; creating an intermediary component i.e
<CardContainer>
that renders a list of cards. This way the<App>
is off the hook when is comes to rendering the<Card>
components.Also, when you use
map
, don’t forget to assign a uniquekey
to your items!