Saltar al contenido principal
← ⚡ JavaScript & the browser·Módulo C3 · Lección 3
TareaInline 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.

Exportación predeterminada versus nombre: cuando cada uno cabe

75 XP7 min
Teoría

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.

🔒

Regístrate para empezar a programar

La teoría está abierta a todos. El editor, la vista previa y el check se desbloquean con una prueba gratis de 7 días — se requiere tarjeta, cancela cuando quieras.

Registrarse — prueba gratis →

Las primeras 10 lecciones de cada ruta son gratis. No se necesita tarjeta.

← AnteriorSiguiente lección →

Recibe un consejo de Python o web al día — por email

Corto, escrito a mano, sin spam. Anula la suscripción en un clic.