I don't understand why this ReactJS code is written like it is. I would like someone to talk me through it bit by bit.
I understand ternary operators and If/Else in JSX and React but not this. Specifically the
if (i > 2) return return child })}
I don't see how that code works (but it does). I mean return then return?
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
function Parent() {
return (
<Head >
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span>7</span>
</Head>
)
}
function Head({children}){
return (
<div>{React.Children.map(children, (child, i) => {
if (i > 2) return
return child
})}
</div>
)
}
ReactDOM.render(<Parent />, document.getElementById('root'));
What I'm doing is trying to learn about the children props. I want to display the first 3 children (which it does) but I don't understand the
map(children, (child, i) => { if (i > 2) return
return child
})}