React Importing and Exporting Components
Last Updated :
18 Dec, 2024
In React, importing and exporting components allows you to share and reuse code across different files. Components can be exported using either default or named exports, and then imported into other files to be used.
1. Default Export and Import
In React, you can use default exports to export a single component from a file. This is a simple way to export one component as the primary item from a file.
Exporting the Component:
JavaScript
// Button.js
import React from 'react';
const Button = () => {
return <button>Click Me!</button>;
};
export default Button; // Default export
Importing the Component
JavaScript
// App.js
import React from 'react';
import Button from './Button'; // Importing default export
const App = () => {
return <Button />;
};
export default App;
Here, the Button component is exported as the default from Button.js. In App.js, it is imported without curly braces and can be named freely.
2. Named Export and Import
With named exports, you can export multiple components from a single file. When importing, you must use the exact name of the exported component inside curly braces.
Exporting Named Components
JavaScript
// Gallery.js
import React from 'react';
export const Profile = () => {
return <img src="your-img-src" alt="Profile" />;
};
export const Gallery = () => {
return (
<section>
<h1>Amazing Scientists</h1>
<Profile />
<Profile />
</section>
);
};
Importing Named Components:
JavaScript
// App.js
import React from 'react';
import { Profile, Gallery } from './Gallery'; // Importing named exports
const App = () => {
return (
<div>
<Gallery />
<Profile />
</div>
);
};
export default App;
In this example, both Profile and Gallery are exported as named exports from Gallery.js. When importing them in App.js, you must use curly braces and match the export names.
3. Combining Default and Named Exports
You can combine default and named exports in the same file. This allows you to export one primary component as default and other components as named exports.
Gallery.js with Both Exports:
JavaScript
// Gallery.js
import React from 'react';
export const Profile = () => {
return <img src="your-img-src" alt="Profile" />;
};
const Gallery = () => {
return (
<section>
<h1>Amazing Scientists</h1>
<Profile />
<Profile />
</section>
);
};
export default Gallery; // Default export
App.js with Both Imports
JavaScript
// App.js
import React from 'react';
import Gallery, { Profile } from './Gallery'; // Importing default and named exports
const App = () => {
return (
<div>
<Gallery />
<Profile />
</div>
);
};
export default App;
In this case, Gallery is exported as the default export while Profile is exported as a named export. When importing, you use the default import syntax for Gallery and the named import syntax for Profile.
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.
Gallery.js with Multiple Exports
JavaScript
// Gallery.js
import React from 'react';
export const Profile = () => {
return <img src="your-img-src" alt="Profile" />;
};
export const Gallery = () => {
return (
<section>
<h1>Amazing Scientists</h1>
<Profile />
<Profile />
</section>
);
};
App.js with all the imports
JavaScript
// App.js
import React from 'react';
import { Profile, Gallery } from './Gallery'; // Importing multiple named exports
const App = () => {
return (
<div>
<Gallery />
<Profile />
</div>
);
};
export default App;
Here, both Profile and Gallery are exported as named exports from Gallery.js, and are imported accordingly in App.js.
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.
React Importing and Exporting Components – FAQ’s
Can I export multiple components from a single file?
Yes, you can use named exports to export multiple components from the same file.
Can a file have both default and named exports?
Yes, a file can have both default exports (one per file) and multiple named exports.
What’s the difference between default and named exports?
Default export is for exporting one primary component per file, while named export allows you to export multiple components or functions.
Can I import a component with a different name?
Yes, when importing a default export, you can give it any name. But for named exports, the import name must match the exported name.
When should I split my components into multiple files?
Split components when they grow too large or when you want to reuse them across different parts of your application. This helps maintain clean and manageable code.
Become a Full-Stack Developer and also get 90% fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.
After successfully processing refunds worth over INR 5 Cr, GeeksforGeeks is back with the Three 90 challenge and this is your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!
Similar Reads
React Importing and Exporting Components
In React, importing and exporting components allows you to share and reuse code across different files. Components can be exported using either default or named exports, and then imported into other files to be used. 1. Default Export and ImportIn React, you can use default exports to export a singl
5 min read
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
Presentational and Container Components in React Redux
React Redux is a library that simplifies state management in React applications by providing a centralized store where data can be stored and accessed from any component. It helps keep your code organized and efficient, especially in large-scale projects where the managing state becomes more complex
2 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
Component composition in React involves the inclusion of one or more components in a single component, where each sub-component can further consist of additional components. Components in React are building blocks that are used to create user interfaces (UIs). Table of Content What is a Component Co
9 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
Why do you need to import React in functional components ?
React is a declarative, efficient, and flexible JavaScript library for building user interfaces. It’s ‘V’ in MVC. ReactJS is an open-source, component-based front-end library responsible only for the view layer of the application. In react, we use JSX, especially in the functional important which is
3 min read
ReactJS Evergreen Button Component
React Evergreen is a popular front-end library with a set of React components for building beautiful products as this library is flexible, sensible defaults, and User friendly. Button Component allows the user to take actions, and make choices, with a single tap. We can use the following approach in
3 min read
React Desktop macOS Link Component
React Desktop is a popular library to bring the native desktop experience to the web. This library provides macOS and Windows OS components. Link Component is used to allow users to add hyperlinks in your application. We can use the following approach in ReactJS to use the React Desktop macOS Link C
2 min read
How to use Portal Component in ReactJS ?
The portal component renders its children into a new subtree outside the current DOM hierarchy. Material UI for React has this component available for us, and it is very easy to integrate. We can use the Portal Component in ReactJS using the following approach. Prerequisites:NodeJS or NPMReact JSMat
2 min read
How to create components in ReactJS ?
Components in React JS is are the core of building React applications. Components are the building blocks that contains UI elements and logic, making the development process easier and reusable. In this article we will see how we can create components in React JS. Table of Content React Functional C
3 min read
What is Components Inheritance in React ?
In React, Component Inheritance refers to one component inheriting the behaviors of another component. It allows objects to have those properties that already exist on previous or parent objects. It is a concept that plays a major role in object-oriented programming. Two classes exist for Component
2 min read
React.js Blueprint Menu Component Props
React.js Blueprint is a front-end UI toolkit. It is very optimized and popular for building interfaces that are complex data-dense for desktop applications. React.js Blueprint Menu Component provides a list of interactive items to the user. React.js Blueprint props are: children: The Menu items serv
3 min read