React Importing and Exporting Components (original) (raw)

Last Updated : 07 Aug, 2025

**In React, components are the building blocks of any **application. They allow developers to break down **complex user interfaces into **smaller, **reusable pieces.

Types of Exports in React

**In React, there are two types of exports

  1. Default Exports and Imports
  2. Named Exports and Imports

1. Default Export and Import

A default export allows you to export a single component or variable from a file. When importing a default export, you can give it any name you choose.

App.js `

import React from "react"; import MyComponent from "./components/MyComponent";

const App = () => { return (

{/* Using the imported component */}
); };

export default App;

MyComponent.js

import React from "react";

const MyComponent = () => { return

Hello from MyComponent!

; };

export default MyComponent;

`

**Output

MyComponent

Default Export and Import

**In this code

2. Named Export and Import

Named exports allow you to export multiple components or variables from a single file. When importing a named export, you must use the exact name of the exported entity.

App.js `

import { MyComponent, AnotherComponent } from "./components/component.js";

const App = () => { return (

); };

export default App;

component.js

export const MyComponent = () => { return

Hello from MyComponent!

; };

export const AnotherComponent = () => { return

Hello from AnotherComponent!

; };

`

**Output

named-export

Named Export and Import

**In this code

3. Combining Default and Named Exports

You can also use both named and default exports in a single file. This is useful when you want to export a primary component (default export) and several utility components or functions (named exports).

App.js `

import { MyComponent, AnotherComponent } from "./components/component.js";

const App = () => { return (

); };

export default App;

component.js

// Named Export export const MyComponent = () => { return

Hello from MyComponent!

; };

// Another Named Export export const AnotherComponent = () => { return

Hello from AnotherComponent!

; };

// Default Export const DefaultComponent = () => { return

Hello from DefaultComponent!

; };

export default DefaultComponent;

`

**Output

combined

Combining Default and Named Exports

4. Exporting Multiple Components from the Same File

You might want to export several components from a single file. React allows you to use named exports for this purpose, and you can only have one default export per file.

App.js `

import React from "react"; import { Header, Footer } from "./components"; import MainContent from "./components";

const App = () => { return (

); };

export default App;

components.js

import React from "react";

// Component 1 export const Header = () => { return

Welcome to My Website

; };

// Component 2 export const Footer = () => { return

; };

// Component 3 (default export) export default function MainContent() { return

This is the main content of the website.
; }

`

**Output

multiple

Exporting Multiple Components from the Same File

When to Use Default Export

When to Use Named Export

Best Practices for Importing and Exporting Components

Conclusion

**Importing and exporting components in **React is essential for building **modular and **maintainable applications. Understanding how to use **default exports and **named exports allows you to efficiently **organize and structure your **React code.

Import/Export Keywords in React