React Importing and Exporting Components
Last Updated :
21 Mar, 2025
In React, components are the building blocks of any application. They allow developers to break down complex user interfaces into smaller, reusable pieces. To manage these components efficiently, you need to understand how to import and export them.
In this article, we will explore the different methods of importing and exporting React components, how to use them, and best practices.
What is Importing and Exporting in React?
In React, importing and exporting components refer to the process of sharing and using components across different files in your application. JavaScript’s import and export syntax allows you to bring in functions, objects, and components from one file into another, creating a modular and maintainable codebase.
- Exporting a component means making it available for use in other files.
- Importing a component means bringing it into another file to use it.
React follows JavaScript’s ES6 module system to handle imports and exports. You can have multiple components in separate files and import them when needed to make your code more modular and maintainable.
Types of Exports in React
In React, there are two types of exports
- Default Exports and Imports
- 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 (
<div>
<MyComponent /> {/* Using the imported component */}
</div>
);
};
export default App;
MyComponent.js
import React from "react";
const MyComponent = () => {
return <h1>Hello from MyComponent!</h1>;
};
export default MyComponent;
Output

Default Export and Import
In this code
- Default Export: Exports one thing from a file (like a component).
- Default Import: Imports the default export from another file, naming it as needed.
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 (
<div>
<MyComponent />
<AnotherComponent />
</div>
);
};
export default App;
component.js
export const MyComponent = () => {
return <h1>Hello from MyComponent!</h1>;
};
export const AnotherComponent = () => {
return <h1>Hello from AnotherComponent!</h1>;
};
Output

Named Export and Import
In this code
- Here, two components (MyComponent and AnotherComponent) are exported individually using the export keyword.
- This allows these components to be available for import in other files.
- We import the specific components (MyComponent and AnotherComponent) from the components.js file using their exact names inside curly braces ({}).
- This is called a Named Import because we are referring to the exact names of the exports.
- Inside the App component, we use both imported components to render them.
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 (
<div>
<MyComponent />
<AnotherComponent />
</div>
);
};
export default App;
component.js
// Named Export
export const MyComponent = () => {
return <h1>Hello from MyComponent!</h1>;
};
// Another Named Export
export const AnotherComponent = () => {
return <h1>Hello from AnotherComponent!</h1>;
};
// Default Export
const DefaultComponent = () => {
return <h1>Hello from DefaultComponent!</h1>;
};
export default DefaultComponent;
Output

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 (
<div>
<Header />
<MainContent />
<Footer />
</div>
);
};
export default App;
components.js
import React from "react";
// Component 1
export const Header = () => {
return <h1>Welcome to My Website</h1>;
};
// Component 2
export const Footer = () => {
return <footer>© 2023 My Website</footer>;
};
// Component 3 (default export)
export default function MainContent() {
return <div>This is the main content of the website.</div>;
}
Output

Exporting Multiple Components from the Same File
When to Use Default Export
- Use for a single primary functionality or component in a file.
- When you want flexibility in naming during import.
- Ideal for components or modules that represent the main purpose of the file.
When to Use Named Export
- Use when exporting multiple functionalities or components from the same file.
- When you want consistency in import names.
- Useful for utility functions, constants, or multiple related components.
Best Practices for Importing and Exporting Components
- Use Default Exports for Primary Components: If a file contains a main component, it’s often a good idea to use default export. This makes it easy to import the primary component without specifying its name.
- Use Named Exports for Utilities and Helper Components: If you have multiple components or utility functions in a file, use named exports. This allows you to import only the necessary components.
- Be Consistent: Stick to a consistent pattern for exports and imports across your project. For example, always use default exports for primary components, and named exports for others.
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.
Similar Reads
How to import components in React Native ?
React Native is a framework developed by Facebook for creating native-style applications for Android & iOS under one common language, i.e. JavaScript. Initially, Facebook only developed React Native to support iOS. However, with its recent support of the Android operating system, the library can
5 min read
React MUI Layout Components
React Material-UI (MUI) is a popular library that provides a set of reusable components for building user interfaces in React applications. These components are based on Material Design, a design system developed by Google that provides guidelines for creating visually appealing, user-friendly inter
2 min read
React JS Component Composition
In React, it becomes difficult to manage the complex UI components and maintain the code. With the help of Component Compositions, we can solve this problem by combining the components into a single component. What is Component Composition?Component composition in React includes one or more componen
5 min read
React JS Component Composition and Nested Components
React JS Component Composition allows you to build complex UIs by combining smaller, reusable components. Nested Components refer to components that are used within other components, enabling a modular and structured approach to building React applications. Table of Content Component CompositionNest
3 min read
React MUI Navigation Components
React Material-UI (MUI) is a popular library that provides a set of reusable components for building user interfaces in React applications. These components are based on Material Design, a design system developed by Google that provides guidelines for creating visually appealing, user-friendly inter
3 min read
ReactJS Container and Presentational Pattern in Components
In this article we will categorise the react components in two types depending on the pattern in which they are written in application and will learn briefly about these two categories. We will also discuss about alternatives to this pattern. Presentational and Container ComponentsThe type of compon
2 min read
React MUI Components
React MUI is a UI library that provides fully-loaded components, bringing our own design system to our production-ready components. MUI is a user interface library that provides predefined and customizable React components for faster and easy web development, these Material-UI components are based o
4 min read
ReactJS UI Ant Design ConfigProvider Component
Ant Design Library has this component pre-built, and it is very easy to integrate as well. ConfigProvider Component is used to provide uniform configuration support for components. We can use the following approach in ReactJS to use the Ant Design ConfigProvider Component. ConfigProvider Props: auto
2 min read
ReactJS UI Ant Design Card Component
Ant Design Library has this component pre-built, and it is very easy to integrate as well. Card Component is used as a simple rectangular container, and it is used when the user wants to display content related to a single subject. We can use the following approach in ReactJS to use the Ant Design C
3 min read
Dumb Components in React Native
In this article, we are going to learn about Dumb components in react Native. The dumb component is one of the categories of React Component so before diving into the dumb components detail. Let's know a little bit about components. A Component is one of the core building blocks of React. The compon
4 min read