Naar hoofdinhoud
← ⚡ JavaScript & the browser·Module C3 · Les 3
OpdrachtInline approximation: declare a default-style 'Button' arrow function and a named-style TIMEOUT_MS constant. Then log 'Button:' + typeof Button and 'TIMEOUT_MS:' + TIMEOUT_MS.

Standaard export versus benoemd: wanneer iedereen past

75 XP7 min
Theorie

Default export — one main thing per file

// Button.js
export default function Button(props) { /* ... */ }

// or
export default class Button { /* ... */ }

// or
const Button = () => { /* ... */ };
export default Button;
// app.js
import Button from "./Button.js";    // no curlies — the importer names it

There's exactly one default export per file. The importer picks the local name — import Btn from "./Button.js" works too. That's the trade-off: convenience vs grep-ability.

Mixing default and named

// http.js
export default function fetchJSON(url) { /* main thing */ }
export const TIMEOUT_MS = 5000;
export function fetchText(url) { /* secondary thing */ }
import fetchJSON, { TIMEOUT_MS } from "./http.js";

The default is the "main thing." Named exports stand for everything else.

When to use which

  • Default — when the file is fundamentally about ONE thing (a React component, a class, the entry point of a library).
  • Named — when the file exports a set of utilities at the same level (format.js exports formatDate, formatCurrency, formatPercent).
  • Avoid mixing default + named in the same file once the named set grows past 2-3 things. Refactor the file into "main + sub-exports" or split it.

React convention

React components use default exports almost universally; hooks use named. That's just convention, not a rule, but it's so universal that breaking it costs reader-attention.

🔒

Registreer om te beginnen met coderen

Theorie is voor iedereen. De editor, live preview en check ontgrendelen met een 7-daagse gratis proefperiode — kaart vereist, opzegbaar.

Registreren — gratis proef →

De eerste 10 lessen in elke track zijn gratis. Geen kaart nodig.

← VorigeVolgende les →

Eén Python- of web-tip per dag — per e-mail

Kort, met de hand geschreven, geen spam. Met één klik afmelden.