I would like to understand why react behaves this way.
This works
class Feed extends React.Component {
constructor(props) {
super(props);
}
render() {
const posts = [{ id: 1, title: 'post-1' }, { id: 2, title: 'post-2' }];
return (
<>
{posts.map(post => (
<Post key={post.id} title={post.title} />
))}
</>
But this doesn't
class Feed extends React.Component {
constructor(props) {
super(props);
}
render() {
const posts = [{ id: 1, title: 'post-1' }, { id: 2, title: 'post-2' }];
return (
<>
{posts.map(post => {
// changes are here
if (post.id < 2) {
<Post key={post.id} title={post.title} />;
}
})}
</>
It just returns blank. No errors.
What is the reason react isn't rendering this? And what would be the best approach to only render post-1
?