0 day streak
javascript / beginner
Snippet

Grouping Elements with Fragments

React components must return a single root element. Fragments (<>...</>) allow you to group multiple elements without adding an extra <div> to the DOM.

snippet.js
javascript
1
2
3
4
5
6
7
8
function Post() {
return (
<>
<h2>Article Title</h2>
<p>Article content goes here.</p>
</>
);
}
react
Breakdown
1
<>
The opening tag for a React Fragment (short syntax).
2
</>
The closing tag for a React Fragment.