

The other one is a new shorter syntax, using empty brackets, that does the same thing. Spectrum's source frequently uses fragment-returning components for this purpose. Fragments can be used using 2 syntax, the first one is using React.Fragment which weve used multiple times above. React Fragments are a neat little feature that were released late last year with React v16.2.0 it’s a pretty small feature, but just knowing about the existence of them can save developers a huge headache when running into some very specific.
Unfortunately container components in ui frameworks deal with arrays of child components like infragments exists as a workaround to return adjacent jsx elements. You can use fragments to create components that return a list of elements without wrapping them in a container or array. if you need to style a fragment then you probably shouldnt be using it in the first place. I’ve taken a look through a few open source projects to see how Fragments are being used in practice - and found a number of common use cases: #1. Of course, you don’t have to worry about any of this if you’re using create-react-app, because it has supported fragments from when they were first released! #Typical usesįragments have now had plenty of time for them to make their way into real world codebases. Therefore, if you want to add props to a wrapper component, switch to a good ol' div. In the future, we may add support for additional attributes, such as event handlers.

But now that a year has passed, most major tools support it, including: As per the React docs on Fragments: key is the only attribute that can be passed to Fragment. When fragments were first released, there wasn’t much support for this syntax.
