To export and import components in React, you mark a component with export in its own file, then bring it into another file with import. A default export is imported without braces under any name you like, while a named export is imported with braces using its exact name. Splitting components into separate files keeps each file small and makes components easy to reuse.
The syntax comes from standard JavaScript modules (ES modules), not from React itself, so everything here applies to any modern JavaScript project. This guide shows both export styles and the rules for choosing between them.
A real React app contains dozens or hundreds of components. Keeping them all in one file quickly becomes unreadable. The usual convention is one main component per file, named after the component, such as Button.jsx or ProductCard.jsx.
For a file to share something, it must export it. For another file to use it, it must import it. Anything you do not export stays private to the file.
A file can have at most one default export. This is the most common style for a component file.
You can also write the export inline:
Import it with no braces. Because the import is a default import, the local name is your choice:
import Btn from "./Button.jsx" also works and gives the component the local name Btn. That freedom is convenient, but it can make a codebase inconsistent, so most teams keep the import name equal to the component name.
A file can have many named exports. You must import each one with its exact name inside braces.
To rename a named import, use as:
| Feature | Default export | Named export |
|---|---|---|
| Exports per file | One | Many |
| Import syntax | import Name from "./file" | import { Name } from "./file" |
| Import name | Any name you choose | Must match, unless you use as |
| Refactoring and autocomplete | Weaker, names can drift | Stronger, names stay in sync |
Lazy loading with React.lazy | Required form | Needs a small wrapper |
Many style guides prefer named exports because editors can rename them across the project and typos become errors. Others prefer one default export per component file. Both are valid; follow your project convention. You can even mix the two in one file, with one default and several named exports, though a file that does both is often a sign it should be split.
The string after from is the module path.
"./Button" or "./Button.jsx" points to a file in the same folder."../components/Button" goes up one folder and into components."react" with no leading dot points to a package installed from npm.Relative paths must start with ./ or ../. Whether you may omit the extension depends on your tooling. Vite and most bundlers resolve .jsx and .tsx without it, while native browser modules and Node ES modules require the full file name.
When a folder holds several components, an index.js file can gather them so other code imports from one place:
This pattern is handy for small component libraries. In large apps, big index files can slow down bundling and hurt tree shaking, so use them with care.
Element type is invalid: expected a string... but got: undefined: you imported a default export with braces, or a named export without braces. Match the import style to the export style.does not provide an export named X: the name in braces does not exist in the file. Check spelling and capitalisation.export, so the import resolves to nothing useful.Yes, with named exports. You can still add one default export alongside them, but keep files focused so they remain easy to read.
No, the compiler allows any name. Using the same name as the component is the convention because it avoids confusion.
Check that it starts with ./ or ../ for local files and that the file name and case match exactly. On case-sensitive systems such as Linux, button.jsx and Button.jsx are different files.
Small helper components used only by one parent can stay in the same file. Anything reused elsewhere should move to its own file and be exported.
Use export default plus import Name from "./file" for one main component per file, and export plus import { Name } from "./file" for several. Keep paths relative, match the import form to the export form, and follow one convention across the project so imports stay predictable.