0

In React version 16, Fragments were implemented. I love using them!

My question is, what if I use Fragments unnecessarily. Does it matter? Can I stick them in as boilerplate for my code in-case I want to add adjacent elements later or is that bad practice.

And if it is bad practice. Why? Since they don't get rendered in the DOM anyway. Does it matter if I add it or not?

It just feels wrong and I want some clarity there.

Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143
  • 1
    I doesn't really matter, but why would you want to do that? You end up with more code unnecessarily and should you add more adjacent code in the future it only takes 2 seconds to add a fragment... – Chris Feb 23 '18 at 07:48
  • Well I have a colleague that keeps telling me it doesn't matter so he keeps adding the stupid boilerplate code. I want to remove them but then he complains that he might (like 0.05% change of that happening) use it later... – Barry Michael Doyle Feb 23 '18 at 09:03
  • 1
    I personally don't like that practice either, but it isn't dangerous or "wrong". It's almost equivalent to having every component render start with a wrapping `div` but with the difference that it doesn't render to DOM. However, depending on your app size and how many components you have, you are increasing your bundle size slightly. – Chris Feb 23 '18 at 09:08

1 Answers1

-1

The use of the Fragment tag will only affect the bundle size of the project in a very minor way.

Best practice would be to only use a Fragment when it is necessary.

Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143