Importing and Exporting Components – React (2024)

The magic of components lies in their reusability: you can create components that are composed of other components. But as you nest more and more components, it often makes sense to start splitting them into different files. This lets you keep your files easy to scan and reuse components in more places.

You will learn

  • What a root component file is
  • How to import and export a component
  • When to use default and named imports and exports
  • How to import and export multiple components from one file
  • How to split components into multiple files

The root component file

In Your First Component, you made a Profile component and a Gallery component that renders it:

function Profile() { return ( <img src="https://i.imgur.com/MK3eW3As.jpg" alt="Katherine Johnson" /> );}export default function Gallery() { return ( <section> <h1>Amazing scientists</h1> <Profile /> <Profile /> <Profile /> </section> );}

These currently live in a root component file, named App.js in this example. Depending on your setup, your root component could be in another file, though. If you use a framework with file-based routing, such as Next.js, your root component will be different for every page.

Exporting and importing a component

What if you want to change the landing screen in the future and put a list of science books there? Or place all the profiles somewhere else? It makes sense to move Gallery and Profile out of the root component file. This will make them more modular and reusable in other files. You can move a component in three steps:

  1. Make a new JS file to put the components in.
  2. Export your function component from that file (using either default or named exports).
  3. Import it in the file where you’ll use the component (using the corresponding technique for importing default or named exports).

Here both Profile and Gallery have been moved out of App.js into a new file called Gallery.js. Now you can change App.js to import Gallery from Gallery.js:

Notice how this example is broken down into two component files now:

  1. Gallery.js:
    • Defines the Profile component which is only used within the same file and is not exported.
    • Exports the Gallery component as a default export.
  2. App.js:
    • Imports Gallery as a default import from Gallery.js.
    • Exports the root App component as a default export.

Note

You may encounter files that leave off the .js file extension like so:

import Gallery from './Gallery';

Either './Gallery.js' or './Gallery' will work with React, though the former is closer to how native ES Modules work.

Deep Dive

Default vs named exports

There are two primary ways to export values with JavaScript: default exports and named exports. So far, our examples have only used default exports. But you can use one or both of them in the same file. A file can have no more than one default export, but it can have as many named exports as you like.

Importing and Exporting Components – React (1)

How you export your component dictates how you must import it. You will get an error if you try to import a default export the same way you would a named export! This chart can help you keep track:

SyntaxExport statementImport statement
Defaultexport default function Button() {}import Button from './Button.js';
Namedexport function Button() {}import { Button } from './Button.js';

When you write a default import, you can put any name you want after import. For example, you could write import Banana from './Button.js' instead and it would still provide you with the same default export. In contrast, with named imports, the name has to match on both sides. That’s why they are called named imports!

People often use default exports if the file exports only one component, and use named exports if it exports multiple components and values. Regardless of which coding style you prefer, always give meaningful names to your component functions and the files that contain them. Components without names, like export default () => {}, are discouraged because they make debugging harder.

Exporting and importing multiple components from the same file

What if you want to show just one Profile instead of a gallery? You can export the Profile component, too. But Gallery.js already has a default export, and you can’t have two default exports. You could create a new file with a default export, or you could add a named export for Profile. A file can only have one default export, but it can have numerous named exports!

Note

To reduce the potential confusion between default and named exports, some teams choose to only stick to one style (default or named), or avoid mixing them in a single file. Do what works best for you!

First, export Profile from Gallery.js using a named export (no default keyword):

export function Profile() {

// ...

}

Then, import Profile from Gallery.js to App.js using a named import (with the curly braces):

import { Profile } from './Gallery.js';

Finally, render <Profile /> from the App component:

export default function App() {

return <Profile />;

}

Now Gallery.js contains two exports: a default Gallery export, and a named Profile export. App.js imports both of them. Try editing <Profile /> to <Gallery /> and back in this example:

import Gallery from './Gallery.js';import { Profile } from './Gallery.js';export default function App() { return ( <Profile /> );}

Now you’re using a mix of default and named exports:

  • Gallery.js:
    • Exports the Profile component as a named export called Profile.
    • Exports the Gallery component as a default export.
  • App.js:
    • Imports Profile as a named import called Profile from Gallery.js.
    • Imports Gallery as a default import from Gallery.js.
    • Exports the root App component as a default export.

Recap

On this page you learned:

  • What a root component file is
  • How to import and export a component
  • When and how to use default and named imports and exports
  • How to export multiple components from the same file

Try out some challenges

Challenge

1

of

1:

Split the components further

Currently, Gallery.js exports both Profile and Gallery, which is a bit confusing.

Move the Profile component to its own Profile.js, and then change the App component to render both <Profile /> and <Gallery /> one after another.

You may use either a default or a named export for Profile, but make sure that you use the corresponding import syntax in both App.js and Gallery.js! You can refer to the table from the deep dive above:

SyntaxExport statementImport statement
Defaultexport default function Button() {}import Button from './Button.js';
Namedexport function Button() {}import { Button } from './Button.js';
// Move me to Profile.js!export function Profile() { return ( <img src="https://i.imgur.com/QIrZWGIs.jpg" alt="Alan L. Hart" /> );}export default function Gallery() { return ( <section> <h1>Amazing scientists</h1> <Profile /> <Profile /> <Profile /> </section> );}

After you get it working with one kind of exports, make it work with the other kind.

Importing and Exporting Components – React (2024)

FAQs

Do I still need to import React for every component? ›

This means that you no longer need to import the React module explicitly, unless you want to use some of its properties or methods directly (e.g. React. Component). It simplifies the code by removing the need for unnecessary imports. It makes the code more readable by reducing the clutter caused by unnecessary imports.

How do I export and import class components in React? ›

Exporting and importing a component
  1. Make a new JS file to put the components in.
  2. Export your function component from that file (using either default or named exports).
  3. Import it in the file where you'll use the component (using the corresponding technique for importing default or named exports).

What are the different exports in React? ›

This code can be functions, objects, values, classes, or React components. The export keyword allows these elements to be used in other JavaScript files, thus making the code reusable and modular. There are two main types of export in React: named export and default export.

How to import React in js file? ›

Importing React in JavaScript (ES5)

To import React in component files using ES5 syntax, we use the require function. Here's an example of how to do this: var React = require('react'); Once we've imported React, we can create a simple React component using the React.

Why don't we need to import React anymore? ›

As of React version 17, you don't need to import the whole React object, so you typically only need to import only the hooks and other specific parts you're using. This makes your code more efficient and lean.

Does anyone still use class components React? ›

Class components are still supported by React, but we don't recommend using them in new code. componentDidUpdate(prevProps, prevState, snapshot?) forceUpdate(callback?)

What is the correct way to import a component in React? ›

Importing a Component
  1. import React, { Component } from 'react';
  2. class Button extends Component {
  3. render() {
  4. // ...
  5. }
  6. }
  7. export default Button; // Don't forget to use export default!
Apr 11, 2021

Can we export multiple components in React? ›

in React JS, but only one element can be exported to another component at a time as a default export. In other words, a file can have only one default export. export default Tesla; In the above code, there is a function called “Tesla”, and this function is exported as a default export.

How does import and export work in React? ›

Exporting a React Component

Importing is possible only if the module or named property to be imported has been exported in its declaration. In React we use the keyword export to export a particular module or a named parameter or a combination. Let us now see the different ways we can use the export operation in React.

What are the 3 biggest exports? ›

The 10 Global Biggest Exporting Industries
  • Global Oil & Gas Exploration & Production. ...
  • Global Car & Automobile Manufacturing. ...
  • Global Pharmaceuticals & Medicine Manufacturing. ...
  • Global Plastic Product & Packaging Manufacturing. ...
  • Global Apparel Manufacturing. ...
  • Global Auto Parts & Accessories Manufacturing.

What are the three types of export? ›

While export channels may take different forms, three major types may be identified: indirect, direct and cooperative export marketing group: Indirect export: this is when the manufacturing company does not take direct care of the exporting activities.

Why do we need to import React? ›

Output: Explanations: The JSX gets internally into many React. createElement() function calls and each of them returns an object as shown above. Now because of this, we need to import React from “react” since internally every JSX is creating a React Component using JSX transformer.

Why can't I import React from React? ›

Why doesn't import { React } from 'react' work? Because there isn't an export named React in the react package. There is only a single default export. If you do this, you'll find that React is undefined .

How do I import a React project? ›

You can do this in two steps:
  1. Set up a JavaScript environment that lets you use the JSX syntax, split your code into modules with the import / export syntax, and use packages (for example, React) from the npm package registry.
  2. Render your React components where you want to see them on the page.

Do I have to create react app for every project? ›

There are plenty of other boilerplates you can download but this one is the most popular. If you are concerned with the size of it, you do not need to run create-react-app every time. You can make a react project yourself quite easily and by doing so you have much more control and understanding of your project.

Why not use React memo for all components? ›

This memoized version of your component will usually not be re-rendered when its parent component is re-rendered, as long as its props have not changed. So, most of the time, we think that if a component is wrapped in React. memo , it will not re-render if its props have not changed.

Should I use React for everything? ›

React is an excellent tool for building websites quickly. However, it is not for everything or everyone. It is not great for performance in general, and JSX could be more concise. Luckily, there are many alternatives, like Vue, Svelte, Preact, and Solid.

Do you need to import React when using JSX? ›

Upgrading to the new transform is completely optional, but it has a few benefits: With the new transform, you can use JSX without importing React.

Top Articles
Latest Posts
Article information

Author: Francesca Jacobs Ret

Last Updated:

Views: 6568

Rating: 4.8 / 5 (68 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Francesca Jacobs Ret

Birthday: 1996-12-09

Address: Apt. 141 1406 Mitch Summit, New Teganshire, UT 82655-0699

Phone: +2296092334654

Job: Technology Architect

Hobby: Snowboarding, Scouting, Foreign language learning, Dowsing, Baton twirling, Sculpting, Cabaret

Introduction: My name is Francesca Jacobs Ret, I am a innocent, super, beautiful, charming, lucky, gentle, clever person who loves writing and wants to share my knowledge and understanding with you.