What are React Fragments and Why Use Them?

🚀 React Toughest Interview Question #17 Q17: What are React fragments and why are they useful? Answer: React fragments let you group multiple elements without adding an extra DOM node. Example: function Example() { return ( <> <h1>Hello</h1> <p>Welcome to React!</p> </> ); } ✅ Equivalent to: <React.Fragment> <h1>Hello</h1> <p>Welcome to React!</p> </React.Fragment> Why use Fragments? Avoid unnecessary <div> wrappers (no “div soup”). Improve performance and semantic HTML structure. Reduce CSS complexity caused by extra DOM elements. Pro Tip: Fragments can also take keys when used in lists: items.map(item => ( <React.Fragment key={item.id}>{item.text}</React.Fragment> )); #React #JavaScript #Frontend #WebDevelopment #InterviewPreparation #ReactJS #UI #TechCareers

To view or add a comment, sign in

Explore content categories