07_Exports and Imports - Maniconserve/React-Wiki GitHub Wiki

ES6 Exports & Imports

JavaScript modules let you split code across files and share only what is needed. There are two types of exports: named and default.


1. Named Exports

You can export multiple things from a single file. Each export has a specific name that the importer must use.

// utils.js

export function add(a, b) { return a + b; }

export function subtract(a, b) { return a - b; }

export const PI = 3.14159;

Importing a Single Named Export

import { add } from './utils';

console.log(add(2, 3)); // 5

Importing Multiple Named Exports

import { add, subtract, PI } from './utils';

console.log(add(2, 3)); // 5 console.log(subtract(5, 2)); // 3 console.log(PI); // 3.14159

The names inside { } must exactly match the exported names.


2. Aliases

If an imported name is too long, conflicts with an existing variable, or you just want a shorter name, use as to create an alias.

import { subtract as minus, PI as circlePI } from './utils';

console.log(minus(10, 4)); // 6 console.log(circlePI); // 3.14159

You can also alias on the export side:

// utils.js
function multiply(a, b) {
  return a * b;
}

export { multiply as times };

import { times } from './utils';

console.log(times(3, 4)); // 12

3. Default Export

Each file can have one default export. It represents the main thing the file provides. No curly braces needed when importing.

// greet.js

export default function greet(name) { return Hello, ${name}!; }

Importing a Default Export

import greet from './greet';

console.log(greet('Arjun')); // Hello, Arjun!

You can give a default import any name you want โ€” it does not have to match the original function name.

import sayHello from './greet'; // โœ… also works

console.log(sayHello('Arjun')); // Hello, Arjun!


4. Importing Named and Default at the Same Time

You can import both in a single statement โ€” the default comes first, outside the { }, and named exports go inside.

// mathUtils.js

export default function add(a, b) { return a + b; }

export function subtract(a, b) { return a - b; }

export const PI = 3.14159;

import add, { subtract, PI } from './mathUtils';

console.log(add(2, 3));      // 5
console.log(subtract(5, 2)); // 3
console.log(PI);             // 3.14159

Quick Reference

Scenario Syntax
Named export export function foo() {}
Named import (single) import { foo } from './file'
Named import (multiple) import { foo, bar, baz } from './file'
Named import with alias import { foo as f } from './file'
Default export export default function foo() {}
Default import import foo from './file'
Default import (any name) import myName from './file'
Default + named together import foo, { bar, baz } from './file'

Rules to Remember

  • A file can have only one default export but many named exports.
  • Named imports must use { } and match the exported name exactly (unless aliased).
  • Default imports have no { } and can be named anything.
  • The default import always comes before { } when combining both.

Part of the React Project Wiki โ€” see also: JSX ยท React Components ยท React Project Structure

# ES6 Exports & Imports

JavaScript modules let you split code across files and share only what is needed. There are two types of exports: named and default.


1. Named Exports

You can export multiple things from a single file. Each export has a specific name that the importer must use.

// utils.js

export function add(a, b) {
  return a + b;
}

export function subtract(a, b) {
  return a - b;
}

export const PI = 3.14159;

Importing a Single Named Export

import { add } from './utils';

console.log(add(2, 3)); // 5

Importing Multiple Named Exports

import { add, subtract, PI } from './utils';

console.log(add(2, 3));      // 5
console.log(subtract(5, 2)); // 3
console.log(PI);             // 3.14159

The names inside { } must exactly match the exported names.


2. Aliases

If an imported name is too long, conflicts with an existing variable, or you just want a shorter name, use as to create an alias.

import { subtract as minus, PI as circlePI } from './utils';

console.log(minus(10, 4)); // 6
console.log(circlePI);     // 3.14159

You can also alias on the export side:

// utils.js
function multiply(a, b) {
  return a * b;
}

export { multiply as times };
import { times } from './utils';

console.log(times(3, 4)); // 12

3. Default Export

Each file can have one default export. It represents the main thing the file provides. No curly braces needed when importing.

// greet.js

export default function greet(name) {
  return `Hello, ${name}!`;
}

Importing a Default Export

import greet from './greet';

console.log(greet('Arjun')); // Hello, Arjun!

You can give a default import any name you want โ€” it does not have to match the original function name.

import sayHello from './greet'; // โœ… also works

console.log(sayHello('Arjun')); // Hello, Arjun!

4. Importing Named and Default at the Same Time

You can import both in a single statement โ€” the default comes first, outside the { }, and named exports go inside.

// mathUtils.js

export default function add(a, b) {
  return a + b;
}

export function subtract(a, b) {
  return a - b;
}

export const PI = 3.14159;
import add, { subtract, PI } from './mathUtils';

console.log(add(2, 3));      // 5
console.log(subtract(5, 2)); // 3
console.log(PI);             // 3.14159

Quick Reference

Scenario Syntax
Named export export function foo() {}
Named import (single) import { foo } from './file'
Named import (multiple) import { foo, bar, baz } from './file'
Named import with alias import { foo as f } from './file'
Default export export default function foo() {}
Default import import foo from './file'
Default import (any name) import myName from './file'
Default + named together import foo, { bar, baz } from './file'

Rules to Remember

  • A file can have only one default export but many named exports.
  • Named imports must use { } and match the exported name exactly (unless aliased).
  • Default imports have no { } and can be named anything.
  • The default import always comes before { } when combining both.

โš ๏ธ **GitHub.com Fallback** โš ๏ธ