
Let’s see them in action while defining a component returning four HTML elements. As seen before, this is the recommended approach, since using any other wrapper tag can lead to invalid HTML. Whenever you have to define a React component that needs to return multiple HTML elements, you should wrap them with the or tag. Wrapping multiple HTML elementsĪs you might have guessed, wrapping multiple HTML elements is the most common use case of React Fragments. There are three use cases where React Fragments are commonly used. On the contrary, see them as a way to avoid unnecessary tags and to get a better markup structure as a result. So, don’t think of Rect Fragments as a replacement for the s in your HTML. īasically, you should use React Fragments any time you would otherwise introduce an unnecessary wrapper to make your component return more than one HTML element. So, keep in mind that the empty tag is a shorthand for.

This leads to the same result as the example above.
#REACT FRAGMENTS CODE#
Here’s the code you might use for this: function Table ( ) Second, such an approach can lead to invalid HTML, as you’re going to see.įor example, let’s say you have a Table component which renders an HTML table, whose columns are rendered with another component called Columns. First, by using this approach consistently, you’re making your DOM more nested, and consequently slower to be rendered. From a logical point of view, this extra can usually be considered irrelevant, but it does have consequences. The easiest solution would be to use a wrapper. This is because React requires that components return only one HTML element. To achieve this, you must wrap all these elements with an HTML tag. Not only do they clean up a lot of component code, they also alleviate all the extra HTML container tags that React applications have been known for.As stated in the official React documentation, returning more than one HTML element is a commonly desired behavior for React components. I just recently discovered the use of Fragments, and I’m already really excited for this straightforward way to eliminate unnecessary, superfluous div elements. It’s also important to note that you won’t be able to pass down any props if you use this special syntax you’d have to use the explicit Fragment component in that case. This is because even though I love opportunities to simplify and shorten my code, I’m not willing to sacrifice readability. I personally prefer the more explicit Fragment syntax. Weird, right? But I guess it comes down to personal preference. These two code blocks produce the same result: const Container = () => ( Hello World! ) const Conatiner = () => ( Hello World! ) React 16.2 also introduced a special fragment syntax that is definitely… something. However… here’s a little something extra weird.

The resulting DOM render from the Container component will look like this: Hello World! Up until React v16, each component had to return only one single element: //will break class Container extends React.Component I started to research easier ways to strip div elements of their properties, and instead found the gorgeous Fragment component introduced in React 16.2.

I would find myself manually overwriting all of these properties in my CSS file, which adds to the already large amount of superfluous lines of code in each app. This can be frustrating when styling and positioning the layout of the app, because all of these extra div’s come with their own inherited block element styling and properties. For this reason, I would see loads of div block elements wrapping around all of my actual content as children when looking at the virtual DOM of my apps. React requires all components to return a single element at a time, which requires developers to wrap everything in either span or div tags. As a React and CSS enthusiast, I started to encounter issues with styling the layout of my React apps.
