DEV Community

Matt Derocher
Matt Derocher

Posted on

2

When to Use React Fragment

I'm sure anyone that's written anything with React has gotten the error Adjacent JSX elements must be wrapped in an enclosing tag. Simply put, a React component can only have one HTML element at its root. So if you want to display an h2tag and a p tag in the same component, you need to wrap them in something. (Part of the reason I love Svelte is that you can put as many elements as you want in the root of a component.) To solve this error most people do this:

const MyComponent = () =>  {
  return (
    <div>
      <h2>Title</h2>
      <p>Some text.</p>
    </div>
  )
}

export default MyComponent
Enter fullscreen mode Exit fullscreen mode

But the problem is that when you just use a lot of divs you create very convoluted DOM output. This image shows the code from one post in a Facebook feed:

Screenshot (192)

The thing is React has built-in syntax that can help fix this. It's the fragment: <>. You can use it in place of a div or other HTML tag. So our above code example becomes:

const MyComponent = () =>  {
  return (
    <>
      <h2>Title</h2>
      <p>Some text.</p>
    </>
  )
}

export default MyComponent
Enter fullscreen mode Exit fullscreen mode

So when should you use a fragment? You should use a fragment anytime you don't need a DOM element for display purposes. The only time you should use a DOM element is when you need it for styling purposes or semantic needs (like wrapping content in an article element).

Why does this matter? Because it's less DOM that has to be rendered and diffed on re-render, so it can be faster. Also modern CSS features like flexbox and grid work so much better when there are fewer elements.

Top comments (2)

Collapse
 
kuyajoe profile image
Kuya Joe • Edited

I just want to hopefully clarify a few things.

The second to the last paragraph is slightly misleading - it's more of - the need for a parent wrapping div disappears, so instead of having divs all over the place (because its required for JSX), you skip that div parent.

Also misleading - re: less DOM.. but react will still create a Virtual DOM. Browser DOM is not the same virtual dom by react. Also, React needs that diffing, even on fragments. I think it would more accurate to say that there's less nested code.

But great article! It's one of the things that showed up on google when looking up when to use the react fragment.

For more info on fragment, also good to check React's doc on it. reactjs.org/docs/fragments.html

Collapse
 
mittyesque profile image
James Thurber

Good to know.

Jetbrains image

Is Your CI/CD Server a Prime Target for Attack?

57% of organizations have suffered from a security incident related to DevOps toolchain exposures. It makes sense—CI/CD servers have access to source code, a highly valuable asset. Is yours secure? Check out nine practical tips to protect your CI/CD.

Learn more

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay