🚀 React: Default Export, Named Export & React Fragments In React development, organizing your code cleanly and consistently makes your projects easier to scale and maintain. Two key parts of this are how you export components and how you structure returned elements. Today I’ll explain default exports, named exports, and React fragments — with a special mention of my mentor’s guiding wisdom. 💡 📦 Default Export vs Named Export in React ✅ Default Export A default export is used when a file exports one main thing — like a primary component. Importing it is simple and doesn’t require curly braces. Example // Header.jsx function Header() { return <h1>Welcome to My App</h1>; } export default Header; Importing: import Header from "./Header"; You can name the import however you like: import MyTitle from "./Header"; 🎯 Named Export Named exports allow you to export multiple things from the same file. When importing, you must use curly braces and the exact exported names. Example // utils.js export function formatDate(date) { return date.toDateString(); } export const APP_NAME = "ReactApp"; Importing: import { formatDate, APP_NAME } from "./utils"; 📄 What Are React Fragments? In React components, you often want to return multiple elements without adding unnecessary wrapper elements to the DOM. That’s where React Fragments come in! Fragments let you group elements without creating extra <div>s. This keeps your DOM cleaner and your CSS simpler. Short Syntax function Card() { return ( <> <h2>Title</h2> <p>Description</p> </> ); } 🎓 What My Mentor Always Said sanjeev ch Write code with intention export what’s necessary, keep your components lean, and avoid clutter where you don’t need it. That advice has helped me understand when to use default exports, when to use named exports, and why React fragments make markup clearer. #ReactJS #JavaScript #WebDevelopment #DefaultExport #NamedExport #ReactFragments #CodingTips #Frontend #MentorWisdom #Programming #LearnToCode

To view or add a comment, sign in

Explore content categories