Mastering React Module Exports

JavaScript Module Exports

Named 'exports'

  • Named exports in JavaScript modules allow developers to export multiple variables, functions, or classes from a module.

  • Each exported item is explicitly named using the export keyword.

  • Imported using curly braces {} in the importing module.

// Module A
export const variableA = 10;
export function funcA() {
  // ...
}
// Module B
import { variableA, funcA } from './ModuleA';

Named 'exports'

  • Default exports enable exporting a single value as the default export for a module.

  • Default item exported as export default item_name at last of the module.

  • The default export is imported without using curly braces {}.

// Module A
const myDefaultExport = 'This is the default export';
export default myDefaultExport;
// Module B
import myDefaultExport from './ModuleA';

**

Mastering JavaScript Module Exports and React Hooks: A Comprehensive Guide**

1. JavaScript Module Exports

Named Export:

  • Introduction:

    • Named exports in JavaScript modules allow developers to export multiple variables, functions, or classes from a module.
  • Usage:

    • Each exported item is explicitly named using the export keyword.

    • Imported using curly braces {} in the importing module.

javascriptCopy code// Module A
export const variableA = 10;
export function funcA() {
  // ...
}
javascriptCopy code// Module B
import { variableA, funcA } from './ModuleA';

Default Export:

  • Introduction:

    • Default exports enable exporting a single value as the default export for a module.
  • Usage:

    • The default export is imported without using curly braces {}.
javascriptCopy code// Module A
const myDefaultExport = 'This is the default export';
export default myDefaultExport;
javascriptCopy code// Module B
import myDefaultExport from './ModuleA';

* asExport:

  • The * as export syntax allows importing all exports from a module as properties of an object.

  • Useful when a module exports multiple values, and you want to access them through a single object.

// Module A
export const varA = 42;
export const funcA = () => { /* ... */ };
// Module B
import * as ModuleA from './ModuleA';
console.log(ModuleA.varA);  // Accessing varA
console.log(ModuleA.funcA); // Accessing funcA