Import and Export Components in React
Learn how to export React components from one file and import them into another to build reusable applications at CIIT Training Institute.
01 Learning Objectives
After completing this lesson, you will be able to:
Learn how to make components available to other files.
Learn how to use components from another module.
Understand the difference between both export types.
Structure components into separate files.
02 What Are Import and Export?
In React, components can be stored in separate JavaScript or JSX
files. The export keyword makes a component or value
available to another module. The import keyword
allows another module to use it.
as.
function Welcome() {
return <h2>Welcome to CIIT Training Institute</h2>;
}
export default Welcome;
import Welcome from "./Welcome";
function App() {
return (
<div>
<Welcome />
</div>
);
}
export default App;
Welcome to CIIT Training Institute
03 Default Export and Import
A default export is commonly used when a file provides one main component. A module can have only one default export.
function CourseTitle() {
return <h2>React JS Training</h2>;
}
export default CourseTitle;
import CourseTitle from "./CourseTitle";
function App() {
return (
<section>
<CourseTitle />
<p>Learn React at CIIT Training Institute.</p>
</section>
);
}
export default App;
React JS Training
Learn React at CIIT Training Institute.
04 Named Export and Import
Named exports allow a module to export multiple components or values. Import them inside curly braces using their exported names.
export function CourseName() {
return <h2>Python Training</h2>;
}
export function CourseDuration() {
return <p>Duration: 5 Months</p>;
}
import {
CourseName,
CourseDuration
} from "./CourseInfo";
function App() {
return (
<section>
<CourseName />
<CourseDuration />
</section>
);
}
export default App;
Python Training
Duration: 5 Months
05 Default Export vs Named Export
Both export types help organize code. Choose the appropriate type according to how you want to expose components from a file.
| Feature | Default Export | Named Export |
|---|---|---|
| Syntax | export default Header; |
export function Header() { ... } |
| Import Syntax | import Header from "./Header"; |
import { Header } from "./Header"; |
| Import Name | Can use another local name | Uses the exported name or an alias |
| Exports per Module | One default export | Multiple named exports |
06 Import Default and Named Exports Together
You can import a default export and named exports from the same module in one import statement.
function StudentInfo() {
return <h2>CIIT Student Information</h2>;
}
export function StudentCourse() {
return <p>Course: React JS</p>;
}
export default StudentInfo;
import StudentInfo, {
StudentCourse
} from "./StudentInfo";
function App() {
return (
<section>
<StudentInfo />
<StudentCourse />
</section>
);
}
export default App;
CIIT Student Information
Course: React JS
07 Organizing Components into Folders
As an application grows, keeping related components in separate folders makes the project easier to understand and maintain.
Reusable UI components
Imports and arranges components
Application entry point in a typical Vite project
import Header from "./components/Header";
import CourseCard from "./components/CourseCard";
import StudentCard from "./components/StudentCard";
function App() {
return (
<div>
<Header />
<CourseCard />
<StudentCard />
</div>
);
}
export default App;
Application Layout
Header component
CourseCard component
StudentCard component
File names and import paths must match your actual project. Some React tools allow omitting the file extension.
08 Practical Example: Student Profiles
In this example, a reusable StudentCard component is exported from one file and imported into another component.
function StudentCard({ name, course }) {
return (
<div>
<h3>{name}</h3>
<p>Course: {course}</p>
</div>
);
}
export default StudentCard;
import StudentCard from "./StudentCard";
function StudentSection() {
return (
<section>
<h2>CIIT Student Profiles</h2>
<StudentCard
name="Samadhan"
course="React JS"
/>
<StudentCard
name="Samiksha"
course=".NET Full Stack"
/>
<StudentCard
name="Pradnya"
course="Python"
/>
</section>
);
}
export default StudentSection;
import StudentSection from "./StudentSection";
function App() {
return <StudentSection />;
}
export default App;
CIIT Student Profiles
09 Common Import and Export Errors
1. Incorrect Import Name
Named exports must match the exported name unless you
provide an alias using as.
import { CourseName } from "./CourseInfo";
2. Incorrect Default Import
Use the default import syntax for a default export. Do not wrap the default import name in curly braces.
import CourseTitle from "./CourseTitle";
3. Incorrect File Path
Verify the folder, file name and letter casing in the import path.
import Header from "./components/Header";
4. Missing Export
A component used by another module must be exported from its file.
export default Header;
10 Practice Questions
- What is the purpose of the export keyword?
- What is the purpose of the import keyword?
- Explain the difference between default and named exports.
- Create a CourseTitle component and import it into App.jsx.
- Create a module that exports two named components.
- Build a StudentSection using Samadhan, Samiksha and Pradnya.
- Identify and correct an incorrect component import path.
Summary
Import and export statements help organize React applications by allowing components and values to be shared across files. A module can have one default export and multiple named exports. Correct import syntax and file paths are essential for avoiding module errors. Separating reusable components such as Header, CourseCard and StudentCard makes applications easier to maintain at CIIT Training Institute.