Popular Searches
Popular Course Categories
Popular Courses

Importing and Exporting Components

Importing and Exporting Components

React Components

Importing and Exporting Components in React JS

Importing and Exporting Components is an important concept in React JS that allows developers to organize application code into separate files and reuse components across different parts of a project. Exporting makes a component available to other files, while importing allows another file to use that component.

As a React application grows, keeping every component in one file can make the code difficult to understand and maintain. By separating components into different files and importing them when needed, developers can build modular, reusable, and well-organized applications.

Course: React JS Training

Course Link: JustAcademy React JS Training Course

Demo Link: Register for React JS Course Demo


1. What Is Exporting in React?

Exporting is a JavaScript module feature that allows a function, variable, class, or component to be made available to other files. In React, components are commonly exported so they can be imported and rendered in other parts of the application.

There are two main types of exports:

  • Default Export: Exports one main value from a module as its default export.
  • Named Export: Exports a value using a specific name. A module can contain multiple named exports.

Example of Exporting a Component

function Header() {
  return <h1>Welcome to My Website</h1>;
}
export default Header;

In this example, the Header component is exported as the default export. Another file can import it and display it in the UI.

2. What Is Importing in React?

Importing allows one JavaScript file to access a value exported by another file. In React, importing is used to access components, functions, constants, and other modules.

Example of Importing a Component

import Header from "./Header";

function App() {
  return (
    <div>
      <Header />
      <p>Welcome to the home page.</p>
    </div>
  );
}
export default App;

The statement import Header from "./Header"; imports the default export from the Header module. The component is then rendered using <Header />.

3. Why Are Importing and Exporting Important?

  • Code reusability: A component can be imported and reused in different files.
  • Code organization: Components can be separated into logical files and folders.
  • Maintainability: Smaller modules are often easier to update and debug.
  • Readability: Developers can understand the purpose of each file more easily.
  • Team collaboration: Different developers can work on different components.
  • Scalability: Applications can grow without putting every component in one large file.
  • Testing: Individual modules and components can be tested separately.

4. Understanding Default Export

A default export identifies the main value that a module exports. A JavaScript module can have only one default export.

Example: Header.jsx

function Header() {
  return (
    <header>
      <h1>My React Website</h1>
    </header>
  );
}
export default Header;

Importing Header.jsx

import Header from "./Header";

function App() {
  return (
    <div>
      <Header />
    </div>
  );
}
export default App;

Important Points

  • A module can have only one default export.
  • A default export is imported without curly braces.
  • The local name used in a default import can differ from the original component name, although keeping meaningful matching names is recommended.
  • Default exports are useful when a file primarily represents one component.

5. Understanding Named Export

A named export makes a value available under a specific exported name. A module can have multiple named exports.

Example: Layout.jsx

export function Header() {
  return <h1>Website Header</h1>;
}
export function Footer() {
  return <footer>Website Footer</footer>;
}
export function Sidebar() {
  return <aside>Website Sidebar</aside>;
}

Importing Named Exports

import { Header, Footer, Sidebar } from "./Layout";

function App() {
  return (
    <div>
      <Header />
      <Sidebar />
      <main>Main Content</main>
      <Footer />
    </div>
  );
}
export default App;

Important Points

  • A module can have multiple named exports.
  • Named imports use curly braces.
  • The imported name must match the exported name unless an alias is used.
  • Named exports are useful when a module provides several related components or utilities.

6. Difference Between Default Export and Named Export

Feature Default Export Named Export
Number per module Only one Multiple allowed
Export syntax export default Header; export function Header() {}
Import syntax import Header from "./Header"; import { Header } from "./Layout";
Curly braces Not required for the import Required for ordinary named imports
Import naming Can choose a different local name Must match the exported name unless aliased
Typical use A module's primary component or value Multiple related components, functions, or constants

7. Creating Separate Component Files

In a React project, components are often stored in separate files inside a components directory. This keeps the project organized as it grows.

Example Project Structure

src/
  components/
    Header.jsx
    Navbar.jsx
    MainContent.jsx
    Footer.jsx
  App.jsx
  main.jsx

Each file can define and export its own component. The App.jsx file can import those components and combine them into the main UI.

Header.jsx

function Header() {
  return <header><h1>My Website</h1></header>;
}
export default Header;

Navbar.jsx

function Navbar() {
  return (
    <nav>
      <a href="/">Home</a>
      {" "}
      <a href="/about">About</a>
      {" "}
      <a href="/contact">Contact</a>
    </nav>
  );
}
export default Navbar;

MainContent.jsx

function MainContent() {
  return (
    <main>
      <h2>Welcome to React JS</h2>
      <p>Learn how to build reusable user interfaces.</p>
    </main>
  );
}
export default MainContent;

Footer.jsx

function Footer() {
  return <footer>Copyright 2026</footer>;
}
export default Footer;

App.jsx

import Header from "./components/Header";
import Navbar from "./components/Navbar";
import MainContent from "./components/MainContent";
import Footer from "./components/Footer";

function App() {
  return (
    <div>
      <Header />
      <Navbar />
      <MainContent />
      <Footer />
    </div>
  );
}
export default App;

When the application renders App, React displays the imported components in the order in which they appear in the JSX.

8. Importing Multiple Named Components from One File

Related components can be exported from the same file and imported together using named imports.

Buttons.jsx

export function PrimaryButton() {
  return <button>Submit</button>;
}
export function SecondaryButton() {
  return <button>Cancel</button>;
}
export function DangerButton() {
  return <button>Delete</button>;
}

App.jsx

import {
  PrimaryButton,
  SecondaryButton,
  DangerButton
} from "./Buttons";

function App() {
  return (
    <div>
      <PrimaryButton />
      <SecondaryButton />
      <DangerButton />
    </div>
  );
}
export default App;

This method is helpful when several small, related components belong together. If the file becomes too large, moving components into separate files can improve readability.

9. Using Default and Named Exports in the Same File

JavaScript allows a module to contain one default export alongside multiple named exports.

Components.jsx

export function Header() {
  return <h1>Website Header</h1>;
}
export function Footer() {
  return <footer>Website Footer</footer>;
}
function AppLayout() {
  return <main>Main Application Layout</main>;
}
export default AppLayout;

Importing Both Export Types

import AppLayout, { Header, Footer } from "./Components";

function App() {
  return (
    <div>
      <Header />
      <AppLayout />
      <Footer />
    </div>
  );
}
export default App;

The default import appears without curly braces, while named imports appear inside curly braces. Using one consistent export style across a project can make imports easier to understand.

10. Renaming Named Imports with Aliases

You can use the as keyword to give an imported named export a different local name. This is useful when names conflict or when a clearer local name is needed.

Example: Components.jsx

export function Button() {
  return <button>Click Me</button>;
}

Importing with an Alias

import { Button as SubmitButton } from "./Components";

function App() {
  return <SubmitButton />;
}
export default App;

The exported name is Button, but the importing file uses the local name SubmitButton.

11. Importing a Default Export with a Different Name

A default export can be imported using a different local name because the import does not need to match the exported identifier.

Profile.jsx

function Profile() {
  return <h2>User Profile</h2>;
}
export default Profile;

App.jsx

import UserProfile from "./Profile";

function App() {
  return <UserProfile />;
}
export default App;

This works, but meaningful names are recommended so that the code remains easy to follow.

12. Importing Components from Nested Folders

The import path depends on the location of the importing file and the location of the component file.

Example Project Structure

src/
  components/
    common/
      Header.jsx
    products/
      ProductCard.jsx
  App.jsx

Import Statements in App.jsx

import Header from "./components/common/Header";
import ProductCard from "./components/products/ProductCard";

function App() {
  return (
    <div>
      <Header />
      <ProductCard />
    </div>
  );
}
export default App;

The dot and slash in ./components/... indicate a relative path from the current file. Check the folder structure and file names carefully when writing import statements.

13. Importing Components from Third-Party Libraries

React projects can also import components and functions from installed packages. For example, a developer may install an icon library or a UI component library and import the features needed by the application.

Example with React

import { useState } from "react";

function Counter() {
  const [count, setCount] = useState(0);
  return (
    <button onClick={() => setCount(count + 1)}>
      Count: {count}
    </button>
  );
}
export default Counter;

Here, useState is a named export imported from the installed React package. The component then uses the Hook to manage state.

14. Importing CSS Files

JavaScript modules can import CSS files supported by the project's build setup. This allows component styles to be included in the application.

App.css

.page-title {
  color: navy;
  font-size: 28px;
}

App.jsx

import "./App.css";

function App() {
  return <h1 className="page-title">Welcome to React</h1>;
}
export default App;

The CSS import makes the stylesheet available to the project's build system. The JSX uses className to apply the CSS class to the heading.

15. Importing and Exporting Utility Functions

Import and export statements are not limited to React components. You can also export utility functions and constants to reuse them in multiple files.

utils.js

export function add(a, b) {
  return a + b;
}
export function multiply(a, b) {
  return a * b;
}

App.jsx

import { add, multiply } from "./utils";

function App() {
  return (
    <div>
      <p>Addition: {add(5, 3)}</p>
      <p>Multiplication: {multiply(5, 3)}</p>
    </div>
  );
}
export default App;

This approach keeps reusable logic separate from UI components.

16. Understanding the Root Component

The root component is the main component from which a particular React UI tree begins. In a simple React project, this is often the App component, which is imported into an entry file such as main.jsx.

main.jsx

import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App";

createRoot(document.getElementById("root")).render(
  <StrictMode>
    <App />
  </StrictMode>
);

This example shows how the entry file imports the root App component and renders it into the DOM element whose ID is root. The exact entry file can differ depending on the project setup.

17. Common Import and Export Errors

Error Cause Solution
Attempting to import a named export as default The import syntax does not match the export. Use curly braces for a named export or change the export appropriately.
Attempting to import a default export as named Curly braces are used for a default export. Use a default import without curly braces.
Incorrect file path The relative path or folder name is wrong. Verify the file location and the import path.
Incorrect capitalization The file name or component name does not match the intended module. Check spelling and capitalization, especially for case-sensitive environments.
Missing export statement The component is not exported but is imported from another file. Add a suitable default or named export.
Duplicate default exports A module attempts to declare more than one default export. Keep one default export and use named exports for additional values.
Missing named import The requested named export does not exist in the module. Check the exported name and the import statement.
Importing a component but not rendering it The component is imported but never used in JSX. Render it using its component tag, such as <Header />.

18. Best Practices for Importing and Exporting

  • Use clear and meaningful names for components, functions, and files.
  • Use a consistent export style across the project whenever practical.
  • Use default exports when a file has one primary exported value.
  • Use named exports when a file provides several related components or utilities.
  • Organize components into folders according to their role or feature.
  • Keep import paths accurate and remove unused imports.
  • Avoid creating unnecessarily large files containing unrelated components.
  • Define components at the top level of a module rather than inside another component.
  • Use reusable components instead of duplicating the same UI structure.
  • Give components meaningful names to improve readability and debugging.

19. Practical Example: Student Management Interface

Create a small student interface using multiple files to practise importing and exporting.

Project Structure

src/
  components/
    Header.jsx
    StudentCard.jsx
    Footer.jsx
  App.jsx

Header.jsx

function Header() {
  return <h1>Student Management System</h1>;
}
export default Header;

StudentCard.jsx

function StudentCard({ name, course }) {
  return (
    <div>
      <h2>{name}</h2>
      <p>Course: {course}</p>
    </div>
  );
}
export default StudentCard;

Footer.jsx

export function Footer() {
  return <footer>Student Portal Footer</footer>;
}

App.jsx

import Header from "./components/Header";
import StudentCard from "./components/StudentCard";
import { Footer } from "./components/Footer";

function App() {
  return (
    <div>
      <Header />
      <StudentCard name="Rahul" course="React JS" />
      <StudentCard name="Priya" course="JavaScript" />
      <Footer />
    </div>
  );
}
export default App;

In this project, Header and StudentCard use default exports, while Footer uses a named export. The App component imports all three and renders them together.

20. Practice Exercises

  1. Create a Navbar.jsx file and export a navigation component using a default export.
  2. Create a Buttons.jsx file containing three named exports: SaveButton, CancelButton, and DeleteButton.
  3. Import all three buttons into App.jsx and render them.
  4. Create a CourseCard.jsx component and export it as the default export.
  5. Pass a course name and price to CourseCard through props.
  6. Create a utility file that exports an add() function and import it into a component.
  7. Practise importing a named export with an alias using the as keyword.
  8. Intentionally change an import statement and observe the error, then correct it.

21. Interview Questions and Answers

Q1. What is importing in React?

Answer: Importing allows a JavaScript module to use components, functions, constants, or other values exported by another module.

Q2. What is exporting in React?

Answer: Exporting makes a value available to other JavaScript modules. Components are commonly exported so they can be reused in other files.

Q3. What is the difference between default and named exports?

Answer: A module can have only one default export but multiple named exports. Default imports omit curly braces, while named imports generally use curly braces.

Q4. Can a file contain multiple components?

Answer: Yes. A file can define multiple components and export them as named exports, or provide one default export along with named exports.

Q5. Can a module have two default exports?

Answer: No. A JavaScript module can have only one default export.

Q6. What does the import path represent?

Answer: It identifies the module to load. A relative path such as ./components/Header starts from the importing file's directory.

Q7. Can a default export be imported with a different name?

Answer: Yes. The importing file can choose a different local identifier for a default export, although meaningful names are recommended.

Q8. Can named imports be renamed?

Answer: Yes. The as keyword can provide an alias, for example import { Button as SubmitButton } from "./Buttons";.

Q9. Why should components be separated into files?

Answer: Separate files can improve code organization, readability, reusability, and maintainability as an application grows.

Q10. Are imports and exports React-specific features?

Answer: No. They are JavaScript module features used by React projects and many other JavaScript applications.

22. Quick Revision Checklist

  • Understand the purpose of importing and exporting.
  • Know how to create a default export.
  • Know how to create and import named exports.
  • Understand the difference between default and named import syntax.
  • Import multiple components from a single module.
  • Use aliases for named imports when appropriate.
  • Organize components into separate files and folders.
  • Import CSS files and reusable utility functions where needed.
  • Recognize and correct common import/export errors.
  • Build a small project using components from multiple files.

23. Conclusion

Importing and Exporting Components helps developers create modular and reusable React applications. Default exports are useful for a module's primary value, while named exports allow several related values to be shared. By learning the correct syntax, managing file paths, and organizing components into separate files, developers can write React code that is easier to understand, test, and maintain.

Explore more through the JustAcademy React JS Training Course.

To learn more about the course, visit Register for React JS Course Demo.

24. Official React Documentation

Previous Next
whatsapp