What is the use of empty tags <> </>?
What is the use of empty tags <> </>?
What is the use of empty tags <> </>?
The empty tags <> </> are the short syntax for a React Fragment. They let a component return several elements without adding an extra node to the DOM.
A component must return a single parent element. Instead of wrapping everything in an unnecessary <div>, use a fragment:
function Info() {
return (
<>
<h2>Title</h2>
<p>Some text</p>
</>
);
}
Only the h2 and p appear in the final HTML. This keeps the DOM clean and avoids breaking layouts such as table rows or flex/grid children. The short form cannot take attributes; if you need a key (e.g. in a list), write <React.Fragment key={id}> instead.
Log in or create a free account to answer this question.