I'm aware that using fragments can help make my code neater, but I'm not entirely sure when it's best to use them over regular div tags. I'd appreciate any tips or experiences on this!
4 Answers
Generally, you should use fragments whenever you can, and reserve divs for when you need them. Keeping your markup clean helps!
If you need the tag to stick around in the DOM after rendering, then definitely go for a div. But if you don’t need it to persist, fragments are the way to go. Most of the time, the choice between the two isn’t a huge deal, though!
Fragments are handy because they're like invisible containers. For example, using
Insightful, thanks!
Use fragments in scenarios where you wouldn't normally drop divs in your HTML. Fun fact: you can even assign keys to fragments when using them in loops, but you'll need to call it as Fragment or React.Fragment, not just . A lot of people miss that! I've seen too many unnecessarily nested divs in designs; using fragments could have simplified things!

Yes, I do want the tag to persist after rendering.