Course contentsShow
← React
Lesson 65 of 2,3962. Setup and Modern ToolingFree lesson

Barrel Exports: index.js for Cleaner Imports

Use index files to re-export multiple modules from a folder, simplifying import statements throughout your app.

Barrel Exports: index.js for Cleaner Imports

What you'll learn: How to use index.js files as "barrels" to simplify your import statements across your React app.

The Problem with Scattered Imports

Imagine you have a components folder with multiple files. Without barrel exports, importing three components looks like this:

import Button from './components/Button/Button';
import Card from './components/Card/Card';
import Modal from './components/Modal/Modal';

That's a lot of typing and three separate file paths to maintain!

What is a Barrel Export?

A barrel is an index.js file that re-exports everything from a folder. Think of it like a shipping warehouse: instead of going to ten different storage rooms, you visit one central location that gives you access to everything.

Create a components/index.js file:

export { default as Button } from './Button/Button';
export { default as Card } from './Card/Card';
export { default as Modal } from './Modal/Modal';

Now your imports become:

import { Button, Card, Modal } from './components';

One path, one line, three components! The index.js acts as the folder's public interface.

When to Use Barrels

Barrels work best for folders with shared, reusable items like your common components folder or a hooks folder. They simplify imports throughout your app and make refactoring easier—if you rename a file, you only update the barrel, not every import.

Avoid barrels for feature-specific code that's rarely imported elsewhere—the extra layer adds complexity without benefit.

Key Takeaway: Barrel exports (index.js files) bundle related exports into a single import point, keeping your import statements clean and maintainable as your React project grows.