DEV Community

Discussion on: 4 Ways to Render JSX Conditionally in React

Collapse
 
moopet profile image
Ben Sinclair

Won't all of these render a div regardless, though?

Collapse
 
rasaf_ibrahim profile image
Rasaf Ibrahim

Here, I am rendering short text (<p> tag) in the conditions to make the example short. But you are not limited to any specific html tag, you can render any html tag, even you can render another component.

Collapse
 
miketalbot profile image
Mike Talbot ⭐

Fragments don't render divs, they are a placeholder that renders nothing at all to the DOM, so the only thing rendered is what is returned by the functions etc.

Collapse
 
moopet profile image
Ben Sinclair

So all these examples, which return a wrapper div with conditional content - does React strip out the empty div automatically?

Thread Thread
 
miketalbot profile image
Mike Talbot ⭐ • Edited

<></> is a Fragment not a <div></div>, so nothing apart from the content is rendered.

Thread Thread
 
moopet profile image
Ben Sinclair

Ah, OP has updated the post. It used to use <div> in all the examples.

Thread Thread
 
miketalbot profile image
Mike Talbot ⭐

Ahhh!!! That explains a lot :)