Learn the different methods of importing & exporting ReactJS components - The Talent500 Blog (2024)

If you are a React JS developer, then you have already encountered keywords at the top of the React component like import and export, or export default. In this article, we are going to take a close look at these keywords.

However, this is one of the most important topics in JavaScript, known as a module. But the main purpose of this article is to understand exporting and importing components in React JS. In addition, near the end of the article, you will find a table detailing the various methods for importing and exporting components in React JS.

After completing this article you will learn:

  • Named export
  • Named import
  • Default export
  • Default import
  • Combine more than one import statement
  • Aliasing the binding

So, without further ado, let’s get into the article!

First and foremost, what is the actual meaning of import and export in general?

  • Import: Imports are goods or services that are brought into one country from another country. Let’s say an Indian wants to buy a Tesla car. Since Tesla doesn’t manufacture its cars in India at present, the person will have to import the car from another country (let’s say, USA).
  • Export: Export means sending goods and services to another country. In the above example, America is sending Tesla cars to India. That means America is exporting its Tesla cars to India.

Therefore, America is exporting Tesla cars, and India is importing them.

Similarly, in React JS, we need to export components, functions, or values in order to use those components in another file (module). To use those exported components in another file, we have to import them first.

So, there are two primary ways to export and import components( or values) in React JS.

  1. Named export and import
  2. Default export and import

We can use one or both of them in order to export and import components.

Jump to

Named export

Suppose we want to export these two functions in another React component (file or module). To export these functions into another component as a named export, we just need to use the export keywords in front of the functions.

The export keyword has made these functions available to other components, allowing them to be used by other React components in the future.

//America.js

// Named import

export function Apple() {

return <h1>I’m sending MacBook.</h1>;

}

// Named import

export function Windows() {

return <h1>I’m sending Windows.</h1>;

}

In the above code, the ‘America.js’ component has two functions that are Apple() and Windows() and in front of these functions, the export keywords are mentioned. That means they will be used later in other React components.

We can have multiple Named exports in a React component, so we can use the curly braces { } to export more than one Named export instead of exporting individually.

See in the below code, I have grouped together all functions and variables inside the curly braces that can be easily accessible in other React components.

//America.js

function Apple() {

return <h1>I’m sending MacBook.</h1>;

}

function Windows() {

return <h1>I’m sending Windows.</h1>;

}

// Exporting all items at one go using curly braces.

// Named import

export { Apple, Windows };

Named import

Once we define a component with the export keyword, we can now access and import those exported functions using the import keyword.

Now we are going to import those functions and variables, which are being exported by the “America.js” component, into a component called “India.js.”

//India.js

// Named import

import { Apple } from “./America.js”;

// Named import

import { Windows } from “./America.js”;

In the above code, the import keywords are used to import the Apple() and Windows() functions, which are exported by the “America.js” file.

Also, we must enclose the functions or values in curly braces {…} and give them the same name as defined from where they are exported. That’s why these imports are called exports. We cannot change the name of the imported bindings (bindings are nothing but functions, values, or classes).

But also we can import multiple bindings into a single line of code, separated by commas ( , ).

//India.js

// Named import

import { Apple, Windows} from “./America.js”;

Now we can use these bindings inside the ‘India.js’ component.

//India.js

// Named import

import { Apple, Windows } from “./America”;

function India() {

return (

<>

<Apple />

<Windows />

</>

);

}

Have a look at the illustration about Named export and import.

Learn the different methods of importing & exporting ReactJS components - The Talent500 Blog (2)

Default Export

The default export is also used for exporting components, values, classes, etc. 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.

Suppose we want to export the “Tesla” component to other components from the “America.js” file as a default export, so the syntax will be like this:

//America.js

function Tesla() {

return <h1>I’m sending the Tesla car.</h1>;

}

// Default export

export default Tesla;

In the above code, there is a function called “Tesla”, and this function is exported as a default export. We just need to put export and default keywords in front of the function.

But we can also omit the name of the function in the above code snippet because the “America.js” file represents the name of the function. Have a look at the syntax below.

//America.js

// Default export

export default function () {

return <h1>I’m sending the Tesla car.</h1>;

}

Default Import

Now we can easily import the above component into other components by following the same technique as the Named import, but there are two main differences.

  • We do not need to wrap the binding inside the curly braces.
  • Can be given any name as the name of that exported binding.

//India.js

// Default import

import Car from “./America.js”;

// Named imports

import { Apple, Windows} from “./America.js”;

function India() {

return (

<>

<Car />

<Apple />

<Windows />

</>

);

}

In the above code, we can put any name in the place of “Car” as the default import. Have a look at the illustration below.

Learn the different methods of importing & exporting ReactJS components - The Talent500 Blog (3)

Combine more than one import statement

As you can see in the above code snippets, I’m importing named and default exported bindings in the two lines of code. But we can also import both ways on the same line of code in a component.

In order to import default and named bindings, we must keep these two main points in mind.

  • The default bindings must come first during importing.
  • The non-default (named) binding must be surrounded by curly braces {…}.

// India.js

// Combined named and default imports

import Car, { Apple, Windows } from “./America”;

function India() {

return (

<>

<Car />

<Apple />

<Windows />

</>

);

}

In the above code, the ‘Car’ is a default import but ‘Apple’ and ‘Windows’ are the Named imports.

Aliasing the binding

We can also change the name of the bindings before exporting them to other components using the “as” keyword.

//America.js

function Windows() {

return <h1>I’m sending Windows.</h1>;

}

// Named export

export { Windows as Microsoft };

In the above code snippet, I have changed the name of the Windows() function to the Microsoft() function using the “as” keyword. And this new alias name will be used when importing it into other components.

Have a look at the code below.

//India.js

// Named import

import { Microsoft } from “./America”;

function India() {

return (

<>

<Microsoft />

</>

);

}

A concise table

StructureExport declarationImport declaration
Namedexport function America() {…}import {America} from ‘./File’
Defaultexport default function() {…}import anyName from ‘./File’

Summary

  • We have seen two ways of exporting data that can be used in other components. To use those data we must first import the bindings according to the type of exports.
  • A default export can be only one in a file or module.
  • But a module can have more than one Named export.

Conclusion

In conclusion, there are two ways to import and export components in React JS, each with its own advantages and use cases. The most common method is the default export, which is simple and efficient for exporting a single component.

While named exports are useful when exporting multiple components or constants from a single module. It is also possible to export components and constants together with the object shorthand syntax.

Also, if you are looking for React JS job please search them on talent500.co

15+

Learn the different methods of importing & exporting ReactJS components - The Talent500 Blog (2024)

FAQs

What is the difference between import and export in React JS? ›

Importing allows using contents from another file, whereas exporting makes the file contents eligible for importing.

How do I import and export 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 types of exports in React JS? ›

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 a module in React? ›

React uses the same features as mentioned above, and you may treat each React Component as a module itself. Thus, it is possible to import/export React Components, and is one of the basic operations to be performed. In React we use the keyword import and from to import a particular module or a named parameter.

What is the difference between import and export with example? ›

Import refers to bringing goods and services from another country to the home country while export refers to selling goods and services from the domestic country to other countries. This is the main difference between import and export.

What is the difference between import and export in JavaScript? ›

Modules in JavaScript use the import and export keywords: import : Used to read code exported from another module. export : Used to provide code to other modules.

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

How to import functions in React? ›

Importing a Function

Importing is like telling ReactJS, "Hey, I need this function from that file". Here's how you import a function: // File: App. js import { sayHello } from './MyFunctions'; // Now you can call sayHello in this file sayHello(); // prints "Hello, world!"

How many types of import and export are there? ›

1) Free Importation: Goods traded in the international market are not regulated or prohibited. 2) Regulated Importation: Government regulatory agencies regulate the goods imported and exported in the country. 3) Restricted Importation: Goods are only allowed in the particular condition instructed by the CMTA.

What are the two types of exports in JavaScript? ›

Every module can have two different types of export, named export and default export. You can have multiple named exports per module but only one default export.

What is import type and export type? ›

import type only imports declarations to be used for type annotations and declarations. It always gets fully erased, so there's no remnant of it at runtime. Similarly, export type only provides an export that can be used for type contexts, and is also erased from TypeScript's output.

When to import React? ›

A: We need to import React in every component file because React provides the necessary functions and objects required to create and manage components. By importing React, we're telling our code to make use of these functions and objects, allowing us to build our components.

How to export functions in React JS? ›

Exporting Function Components in React

Here's how we can do it: function Welcome(props) { return <h1>Hello, {props.name}</h1>; } export default Welcome; In this code, export default Welcome; is making the Welcome function accessible to other modules.

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.

What is the difference between exported and imported data? ›

Understanding Data Import and Export

Data import refers to the process of bringing data into a system or application from an external source. Conversely, data export involves transferring data from a system or application to an external destination.

What is the difference between require and import in React JS? ›

One of the major differences between require() and import() is that require() can be called from anywhere inside the program whereas import() cannot be called conditionally, it always runs at the beginning of the file. To use the require() statement, a module must be saved with . js extension as opposed to .

Why does React use import? ›

This was necessary because React components are defined using JSX syntax, which needs to be transformed into regular JavaScript code by the Babel compiler. The React module contained the functions needed to transform JSX into regular JavaScript, so it had to be imported in every file that contained JSX.

What is the difference between imports and net exports? ›

A net exporter, by definition, runs a current account surplus in aggregate. A net importer, by contrast, is a country or territory whose value of imported goods and services is higher than its exported goods and services over a given period of time. By definition, a net importer runs a current account deficit.

Top Articles
Latest Posts
Article information

Author: Fredrick Kertzmann

Last Updated:

Views: 6344

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Fredrick Kertzmann

Birthday: 2000-04-29

Address: Apt. 203 613 Huels Gateway, Ralphtown, LA 40204

Phone: +2135150832870

Job: Regional Design Producer

Hobby: Nordic skating, Lacemaking, Mountain biking, Rowing, Gardening, Water sports, role-playing games

Introduction: My name is Fredrick Kertzmann, I am a gleaming, encouraging, inexpensive, thankful, tender, quaint, precious person who loves writing and wants to share my knowledge and understanding with you.