1. Basic concepts
1.1 The role of Fragment
Fragment allows you to combine lists of child elements without adding additional nodes to the DOM. It solves the limitation that React components must have a single element.
1.2 Two syntax forms
// 1. Explicit Fragment syntaximport React, { Fragment } from 'react'; function ExampleWithFragment() { return ( <Fragment> <h1>Title</h1> <p>Paragraph</p> </Fragment> ); } // 2. Phrase syntax (empty tag)function ExampleWithShortSyntax() { return ( <> <h1>Title</h1> <p>Paragraph</p> </> ); }
2. The difference between Fragment and empty tags
2.1 key attribute support
// Fragment can receive key attributesfunction ListItems({ items }) { return ( <dl> {(item => ( <Fragment key={}> <dt>{}</dt> <dd>{}</dd> </Fragment> ))} </dl> ); } // Empty tags do not support any attributes, including key// This will cause an errorfunction InvalidExample({ items }) { return ( <dl> {(item => ( <key={}> {/* mistake! Not supported attributes */} <dt>{}</dt> <dd>{}</dd> </> ))} </dl> ); }
2.2 Syntax Support
// Fragment needs to be importedimport React, { Fragment } from 'react'; // Empty tags do not need to be imported, use them directlyfunction NoImportNeeded() { return ( <> <div>Item 1</div> <div>Item 2</div> </> ); }
3. Use scenarios
3.1 List Rendering
// Render the list using Fragment (key required)function List({ items }) { return ( <ul> {(item => ( <Fragment key={}> <li>{}</li> <li>{}</li> </Fragment> ))} </ul> ); } // Simple combination (using empty tags)function SimpleComponent() { return ( <> <header /> <main /> <footer /> </> ); }
3.2 Table structure
// Use Fragment in the tablefunction TableRow({ data }) { return ( <Fragment> <td>{}</td> <td>{}</td> <td>{}</td> </Fragment> ); } function Table({ items }) { return ( <table> <tbody> {(item => ( <tr key={}> <TableRow data={item} /> </tr> ))} </tbody> </table> ); }
3.3 Conditional Rendering
function ConditionalRender({ isLoggedIn }) { return ( <> <Header /> {isLoggedIn ? ( <> <UserDashboard /> <UserSettings /> </> ) : ( <> <LoginForm /> <RegisterLink /> </> )} <Footer /> </> ); }
4. Performance considerations
4.1 DOM structure
// Using Fragment or empty tags will not generate additional DOM nodesfunction OptimizedStructure() { return ( <> <div>First</div> <div>Second</div> </> ); } // Rendering result:// <div>First</div> // <div>Second</div> // instead:// <div> // <div>First</div> // <div>Second</div> // </div>
4.2 Memory usage
// Neither Fragment nor empty tags create additional DOM nodes, so there is less memory usagefunction MemoryEfficient() { return ( <> {({ length: 1000 }).map((_, index) => ( <Fragment key={index}> <span>Item</span> <span>Description</span> </Fragment> ))} </> ); }
5. Best Practices
5.1 Select a suggestion
Use empty tags (<>)</>) when:
- No need to pass key attributes
- Pursuing concise code
- Just a simple package function is required
Use Fragment when:
- Need to use the key attribute (such as in the list)
- Need clear semantics
- Need explicit types in TypeScript
5.2 Code Style
// Recommended: Keep consistent indentationfunction GoodStyle() { return ( <> <div>Item 1</div> <div>Item 2</div> </> ); } // Not recommended: chaotic structurefunction BadStyle() { return <> <div>Item 1</div> <div>Item 2</div> </>; }
6. FAQs and Solutions
6.1 TypeScript Support
// Use Fragment in TypeScriptimport React, { Fragment } from 'react'; interface Props { items: Array<{ id: string; text: string }>; } function TypeScriptExample({ items }: Props) { return ( <> {(item => ( <Fragment key={}> <div>{}</div> </Fragment> ))} </> ); }
6.2 Nested Use
// Fragment can be used in nestfunction NestedFragments() { return ( <> <div>Level 1</div> <Fragment> <div>Level 2.1</div> <> <div>Level 3.1</div> <div>Level 3.2</div> </> <div>Level 2.2</div> </Fragment> </> ); }
7. Summary
7.1 Use scenario comparison
Fragment:
When a key attribute is required, it is necessary to clarify the code structure in TypeScript that needs to be semanticized.
Empty tags:
Simple component wrapping does not require any attributes to pursue concise code 7.2 Best practice recommendations Prioritize the use of empty tag syntax when using keys. Use Fragment to keep the code style consistent and pay attention to performance optimization.
This is the end of this article about the detailed and differences between React Fragment and empty tags (<></>). For more related React Fragment empty tags (<></>), please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!