Course contentsShow
← React
Lesson 210 of 2,3966. Component BasicsFree lesson

Index Files for Cleaner Imports

Use index.js barrel files to export multiple components from a folder, enabling cleaner import statements throughout your app.

Index Files for Cleaner Imports

What you'll learn: How to use index.js barrel files to simplify import statements when working with multiple components in the same folder.

The Import Clutter Problem

When you organize components into folders, your imports can get messy fast:

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

Notice how you're repeating folder and file names? This is verbose and tedious.

The Barrel File Pattern

An index file (commonly called a "barrel file") acts like a central export hub for a folder. It re-exports everything you want to make available from that location.

The Setup:

components/
  Button/
    Button.js
    index.js
  Input/
    Input.js
    index.js
  index.js

In Button/index.js:

export { default } from './Button';

In components/index.js:

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

Now your imports become:

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

How It Works

Think of index.js like a reception desk at an office building. Instead of needing the exact room number for each person, you just tell the reception desk who you need, and they direct you to the right place. The barrel file is that reception desk—it knows where everything lives and hands it to you cleanly.

When JavaScript sees import from './components', it automatically looks for index.js in that folder.

Key Takeaway: Index files let you bundle multiple component exports from a folder into a single, clean import statement, reducing repetitive path names and making your code more maintainable.