bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/React/React Core
React•React Core

React ES6 Modules

Modules

JavaScript modules allow you to break up your code into separate files.

This makes it easier to maintain the code-base.

ES Modules rely on the import and export statements.

Export

You can export a function or variable from any file.

Let us create a file named person.js , and fill it with the things we want to export.

There are two types of exports: Named and Default.

Named Exports

You can create named exports two ways:

In-line individually

export const name = "Tobias"
export const age = 18

All at once at the bottom

const name = "Tobias"
const age = 18
export { name, age }

Default Exports

Let us create another file, named message.js , and use it for demonstrating default export.

You can only have one default export in a file.

Example

Insert the following code into the newly created file:

const message = () => {
 const name = "Tobias";
 const age = 18;
 return name + ' is ' + age + 'years old.';
};
export default message;

Import

You can import modules into a file in two ways, based on if they are named exports or default exports.

Named exports must be destructured using curly braces. Default exports do not.

import { name, age } from "./person.js";
import message from "./message.js";

Previous

React ES6 Spread Operator

Next

React ES6 Ternary Operator

This chapter

Overview
45

Lessons

142m

Read time

1. React Tutorial2. React Introduction3. React Getting Started4. Your First React App5. React Render HTML6. Upgrade React7. React ES68. React ES6 Classes9. React ES6 Arrow Functions10. React ES6 Variables11. JavaScript Array map()12. React ES6 Destructuring13. React ES6 Spread Operator14. React ES6 Modules15. React ES6 Ternary Operator16. React ES6 Template Strings17. React JSX18. React JSX Expressions19. React JSX Attributes20. React JSX If Statements21. React Components22. React Class Components23. React Props24. React Destructuring Props25. React Props Children26. React Events27. React Conditional Rendering28. React Lists29. React Forms30. React Submit Forms31. React Forms - Textarea32. React Forms - Select33. React Forms - Multiple Input Fields34. React Forms - Checkbox35. React Forms - Radio36. React Portals37. React Suspense38. Styling React Using CSS39. React CSS Modules40. React CSS-in-JS41. React Router42. React Transitions43. React forwardRef44. React Higher Order Components45. React Sass Styling

On this page

ModulesExportNamed ExportsDefault ExportsImport