export

Defination

The export statement is used to export functions, objects or primitives from a given file (or module).

Syntax

1
export { name1, name2,, nameN };
2
export { variable1 as name1, variable2 as name2,, nameN };
3
export let name1, name2,, nameN; // also var
4
export let name1 =, name2 =,, nameN; // also var, const
5
6
export default expression;
7
export default function () {} // also class, function*
8
export default function name1() {} // also class, function*
9
export { name1 as default,};
10
11
export * from …;
12
export { name1, name2,, nameN } from …;
13
export { import1 as name1, import2 as name2,, nameN } from …;
Copied!

Examples

1
// module "my-module.js"
2
function cube(x) {
3
return x * x * x;
4
}
5
const foo = Math.PI + Math.SQRT2;
6
export { cube, foo };
7
// module "my-module.js"
8
export default function cube(x) {
9
return x * x * x;
10
}
Copied!

Usage

1
import React from 'react';
2
3
export const ADD_TODO = 'ADD_TODO';
4
5
export const addTodo = (content) => ({
6
type: actionType,
7
content
8
});
9
10
const App = () => {
11
return (
12
<div>
13
App
14
</div>
15
);
16
};
17
18
export default App;
Copied!

References