IMO:
If your gonna return an empty fragment you might as well return null.
And if your goal is readability, you can always use multiple return like this
functionList(props){constitems=props.items;// check if items is not emptyconstisEmpty=!(items&&items.length>0);if(isEmpty)returnnullreturn(<ul>{items.map(i=><likey={i}>{i}</li>)}
</ul>
);}
This is exactly what I came to the comments hoping to post, thanks for getting there first!
I'm baffled to see such a simple example turned into such a lot of code and it exemplifies the over-engineering the React community appears to deal with. Replace null with <React.Fragment /> if you must but creating a list shouldn't take 10s of lines of code.
IMO:
If your gonna return an empty fragment you might as well return
null
.And if your goal is readability, you can always use multiple
return
like thisThis is exactly what I came to the comments hoping to post, thanks for getting there first!
I'm baffled to see such a simple example turned into such a lot of code and it exemplifies the over-engineering the React community appears to deal with. Replace
null
with<React.Fragment />
if you must but creating a list shouldn't take 10s of lines of code.I prefer this over returning fragment.
But in the most situations we need an empty state component instead of null
Multiple returns which both return the same type (html) is the most readable and reusable