I started writing react.js code last March and the journey has been fun so far. But I couldn't help but notice that wrapping return values in my code with a "div tag" felt weird. So here's what I found:
- React return values can only take a single value so it is necessary to wrap all the JSX in an Html element.
- "div" being the most popular comes to mind easily due to its versatility and ability to use anywhere.
- Using "div" is semantically wrong as it involves your final code creating an extra DOM node which sometimes complicates everything in JavaScript.
- There is an option of using and empty <> to wrap the elements. This makes sense, especially in cases where your component is returning some part of a table. Wrapping table "td" or "tr" values in a div can destroy its structure since "div" tags are block level elements.
- React has an option of using React.Fragment or "Fragment" tag to group these multiple elements.
- It is better to use "Fragment" tag especially when the code is mapping a collection of items. An empty <> tag cannot take a key value which is necessary after mapping in react. Conclusion: Use "Fragment" React documentation on Fragments