DEV Community

Discussion on: Are you giving the proper sense to React.Fragment?

Collapse
 
aleksandrhovhannisyan profile image
Aleksandr Hovhannisyan

The title is clickbait and React.Fragment (<></>) has very valid use cases. The example you showed technically doesn't need to return a fragment—you can just do return !isEmpty && (...). Or just return null if it's empty, depending on what your preference is.

There are other valid use cases for fragments, too, like if you need to return two siblings but without introducing unnecessary wrapper parents.

Collapse
 
codbugs profile image
Coding Bugs

My apologies if you feel the title is clickbait. That wasn't my intention at all.

My article wants to point out that we should use the tools provided by frameworks and libraries in the scope of our domain solution. Returning React.Fragment as an empty list is something valid and will work but it doesn't mean nothing if we, as programmers, don't give it a meaning. Everything depends on requirements and, maybe, we don't need to deep dive in a big file structure with lots of components because it increase complexity.