Skip to content

Commit 7278e1c

Browse files
authored
translate Importing and Exporting Components (#541)
1 parent 2db750b commit 7278e1c

File tree

2 files changed

+62
-62
lines changed

2 files changed

+62
-62
lines changed

beta/src/pages/learn/importing-and-exporting-components.md

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
---
2-
title: Importing and Exporting Components
2+
title: Importar y exportar componentes
33
---
44

55
<Intro>
66

7-
The magic of components lies in their reusability: you can create components that are composed of other components. But as you nest more and more components, it often makes sense to start splitting them into different files. This lets you keep your files easy to scan and reuse components in more places.
7+
La magia de los componentes reside en su reusabilidad: puedes crear componentes que se componen a su vez de otros componentes. Pero mientras anidas más y más componentes, a menudo tiene sentido comenzar a separarlos en diferentes archivos. Esto permite que tus archivos se mantengan fáciles de localizar y puedas reutilizar componentes en más lugares.
88

99
</Intro>
1010

1111
<YouWillLearn>
1212

13-
* What a root component file is
14-
* How to import and export a component
15-
* When to use default and named imports and exports
16-
* How to import and export multiple components from one file
17-
* How to split components into multiple files
13+
* Qué es un archivo de componente raíz
14+
* Cómo importar y exportar un componente
15+
* Cuándo usar imports y exports *defaults* o con nombre
16+
* Cómo importar o exportar múltiples componentes de un archivo
17+
* Cómo separar componentes en múltiples archivos
1818

1919
</YouWillLearn>
2020

21-
## The root component file {/*the-root-component-file*/}
21+
## El archivo de componente raíz {/*the-root-component-file*/}
2222

23-
In [Your First Component](/learn/your-first-component), you made a `Profile` component and a `Gallery` component that renders it:
23+
En [Tu primer componente](/learn/your-first-component), hiciste un componente `Profile` y un componente `Gallery` que lo renderiza:
2424

2525
<Sandpack>
2626

@@ -52,17 +52,17 @@ img { margin: 0 10px 10px 0; height: 90px; }
5252

5353
</Sandpack>
5454

55-
These currently live in a **root component file,** named `App.js` in this example. In [Create React App](https://create-react-app.dev/), your app lives in `src/App.js`. Depending on your setup, your root component could be in another file, though. If you use a framework with file-based routing, such as Next.js, your root component will be different for every page.
55+
Estos viven actualmente en este ejemplo en un **archivo de componente raíz,** llamado `App.js`. En [Create React App](https://create-react-app.dev/), tu aplicación vive en `src/App.js`. No obstante, en dependencia de tu configuración, tu componente raíz podría estar en otro archivo. Si utilizas un framework con enrutamiento basado en archivos, como Next.js, tu componente raíz será diferente para cada página.
5656

57-
## Exporting and importing a component {/*exporting-and-importing-a-component*/}
57+
## Exportar e importar un componente {/*exporting-and-importing-a-component*/}
5858

59-
What if you want to change the landing screen in the future and put a list of science books there? Or place all the profiles somewhere else? It makes sense to move `Gallery` and `Profile` out of the root component file. This will make them more modular and reusable in other files. You can move a component in three steps:
59+
¿Y si quisieras cambiar la pantalla de inicio en el futuro y poner allí una lista de libros científicos? ¿O ubicar todos los perfiles en otro lugar? Tiene sentido mover `Gallery` y `Profile` fuera del componente raíz. Esto los haría más modulares y reutilizables en otros archivos. Puedes mover un componente en tres pasos:
6060

61-
1. **Make** a new JS file to put the components in.
62-
2. **Export** your function component from that file (using either [default](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/export#using_the_default_export) or [named](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/export#using_named_exports) exports).
63-
3. **Import** it in the file where you’ll use the component (using the corresponding technique for importing [default](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import#importing_defaults) or [named](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import#import_a_single_export_from_a_module) exports).
61+
1. **Crea** un nuevo archivo JS para poner los componentes dentro.
62+
2. **Exporta** tu componente de función desde ese archivo (ya sea usando exports [por defecto](https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Statements/export#usando_el_export_por_defecto) o [con nombre](https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Statements/export#syntax)).
63+
3. **Impórtalo** en el archivo en el que usarás el componente (usando la técnica correspondiente de importar exports [por defecto](https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Statements/import#importaci%C3%B3n_de_elementos_por_defecto) o [con nombre](https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Statements/import#importa_un_solo_miembro_de_un_m%C3%B3dulo.)).
6464

65-
Here both `Profile` and `Gallery` have been moved out of `App.js` into a new file called `Gallery.js`. Now you can change `App.js` to import `Gallery` from `Gallery.js`:
65+
Aquí tanto `Profile` y `Gallery` se han movido fuera de `App.js` en un nuevo archivo llamado `Gallery.js`. Ahora puedes cambiar `App.js` para importar `Gallery` desde `Gallery.js`:
6666

6767
<Sandpack>
6868

@@ -104,76 +104,76 @@ img { margin: 0 10px 10px 0; height: 90px; }
104104

105105
</Sandpack>
106106

107-
Notice how this example is broken down into two component files now:
107+
Nota cómo este ejemplo está ahora descompuesto en dos archivos:
108108

109109
1. `Gallery.js`:
110-
- Defines the `Profile` component which is only used within the same file and is not exported.
111-
- Exports the `Gallery` component as a **default export**.
110+
- Define el componente `Profile` que se usa solo dentro del mismo archivo y no se exporta.
111+
- Define el componente `Gallery` como un *export por defecto*.
112112
2. `App.js`:
113-
- Imports `Gallery` as a **default import** from `Gallery.js`.
114-
- Exports the root `App` component as a **default export**.
113+
- Importa `Gallery` como un **import por defecto** desde `Gallery.js`.
114+
- Exporta el componente raíz `App` como un **export por defecto**.
115115

116116

117117
<Note>
118118

119-
You may encounter files that leave off the `.js` file extension like so:
119+
Puede que te encuentres archivos que omiten la extensión de archivo `.js` de esta forma:
120120

121121
```js
122122
import Gallery from './Gallery';
123123
```
124124

125-
Either `'./Gallery.js'` or `'./Gallery'` will work with React, though the former is closer to how [native ES Modules](https://developer.mozilla.org/docs/Web/JavaScript/Guide/Modules) work.
125+
Tanto `'./Gallery.js'` como `'./Gallery'` funcionarán con React, aunque la primera forma es más cercana a cómo lo hacen los [módulos nativos de ES](https://developer.mozilla.org/es/docs/Web/JavaScript/Guide/Modules).
126126

127127
</Note>
128128

129-
<DeepDive title="Default vs Named Exports">
129+
<DeepDive title="Exports por defecto vs. con nombre">
130130

131-
There are two primary ways to export values with JavaScript: default exports and named exports. So far, our examples have only used default exports. But you can use one or both of them in the same file. **A file can have no more than one _default_ export, but it can have as many _named_ exports as you like.**
131+
Hay dos formas fundamentales de exportar valores con JavaScript: exports por defecto y exports con nombre. Hasta ahora nuestros ejemplos solo han usado exports por defecto. Pero puedes usar uno o ambos en el mismo archivo. **Un archivo no puede tener más de un export _por defecto_, pero puede tener tantos exports _con nombre_ como desees.**
132132

133-
![Default and named exports](/images/docs/illustrations/i_import-export.svg)
133+
![Exports por defecto y con nombre](/images/docs/illustrations/i_import-export.svg)
134134

135-
How you export your component dictates how you must import it. You will get an error if you try to import a default export the same way you would a named export! This chart can help you keep track:
135+
Cómo exportas tu componente dicta la forma en que debes importarlo. ¡Tendrás un error si intentas importar un export por defecto de la misma forma que lo harías con un export con nombre! Este cuadro te puede ayudar a recordarlo:
136136

137-
| Syntax | Export statement | Import statement |
137+
| Sintaxis | Sentencia export | Sentencia import |
138138
| ----------- | ----------- | ----------- |
139-
| Default | `export default function Button() {}` | `import Button from './button.js';` |
140-
| Named | `export function Button() {}` | `import { Button } from './button.js';` |
139+
| Por defecto | `export default function Button() {}` | `import Button from './button.js';` |
140+
| Con nombre | `export function Button() {}` | `import { Button } from './button.js';` |
141141

142-
When you write a _default_ import, you can put any name you want after `import`. For example, you could write `import Banana from './button.js'` instead and it would still provide you with the same default export. In contrast, with named imports, the name has to match on both sides. That's why they are called _named_ imports!
142+
Cuando escribes un import _por defecto_ puedes poner cualquier nombre después de `import`. Por ejemplo, podrías escribir en su lugar `import Banana from './button.js'` y aun así te daría el mismo export por defecto. En cambio, con los imports con nombre, tiene que haber una correspondencia con los nombres en ambos lados. ¡Por eso se llaman exports _con nombre_!
143143

144-
**People often use default exports if the file exports only one component, and use named exports if it exports multiple components and values.** Regardless of which coding style you prefer, always give meaningful names to your component functions and the files that contain them. Components without names, like `export default () => {}`, are discouraged because they make debugging harder.
144+
**Las personas a menudo utilizan exports por defecto si el archivo solo exporta un componente, y usan exports con nombre si exporta varios componentes y valores.** Independientemente del estilo de codificación que prefieras, siempre proporciona nombres con sentido a las funciones de tus componentes y a los archivos que las contienen. Componentes sin nombre como `export default () => {}` no se recomiendan, porque hacen que la depuración sea más difícil.
145145

146146
</DeepDive>
147147

148-
## Exporting and importing multiple components from the same file {/*exporting-and-importing-multiple-components-from-the-same-file*/}
148+
## Exportar e importar múltiples componentes del mismo archivo {/*exporting-and-importing-multiple-components-from-the-same-file*/}
149149

150-
What if you want to show just one `Profile` instead of a gallery? You can export the `Profile` component, too. But `Gallery.js` already has a *default* export, and you can't have _two_ default exports. You could create a new file with a default export, or you could add a *named* export for `Profile`. **A file can only have one default export, but it can have numerous named exports!**
150+
¿Y si quisieras mostrar solo un `Profile` en lugar de toda la galería? Puedes exportar el componente `Profile` también. Pero `Gallery.js` ya tiene un export *por defecto*, y no puedes tener _dos_ exports por defecto. Podrías crear un nuevo archivo con un export por defecto, o podrías añadir un export *con nombre* para `Profile`. **¡Un archivo solo puede contener un export por defecto, pero puede tener múltiples exports con nombre!**
151151

152-
> To reduce the potential confusion between default and named exports, some teams choose to only stick to one style (default or named), or avoid mixing them in a single file. It's a matter of preference. Do what works best for you!
152+
> Para reducir la potencial confusión entre exports por defecto y con nombre, algunos equipos escogen utilizar solo un estilo (por defecto o con nombre), o evitan mezclarlos en un mismo archivo. Es una cuestión de preferencias. ¡Haz lo que funcione mejor para ti!
153153
154-
First, **export** `Profile` from `Gallery.js` using a named export (no `default` keyword):
154+
Primero, **exporta** `Profile` desde `Gallery.js` usando un export con nombre (sin la palabra clave `default`):
155155

156156
```js
157157
export function Profile() {
158158
// ...
159159
}
160160
```
161161

162-
Then, **import** `Profile` from `Gallery.js` to `App.js` using a named import (with the curly braces):
162+
Luego, **importa** `Profile` desde `Gallery.js` hacia `App.js` usando un export con nombre (con llaves):
163163

164164
```js
165165
import { Profile } from './Gallery.js';
166166
```
167167

168-
Finally, **render** `<Profile />` from the `App` component:
168+
Por último, **renderiza** `<Profile />` en el componente `App`:
169169

170170
```js
171171
export default function App() {
172172
return <Profile />;
173173
}
174174
```
175175

176-
Now `Gallery.js` contains two exports: a default `Gallery` export, and a named `Profile` export. `App.js` imports both of them. Try editing `<Profile />` to `<Gallery />` and back in this example:
176+
Ahora `Gallery.js` contiene dos exports: un export por defecto `Gallery`, y un export con nombre `Profile`. `App.js` importa ambos. Intenta editar `<Profile />` cambiándolo a `<Gallery />` y viceversa en este ejemplo:
177177

178178
<Sandpack>
179179

@@ -216,47 +216,47 @@ img { margin: 0 10px 10px 0; height: 90px; }
216216

217217
</Sandpack>
218218

219-
Now you're using a mix of default and named exports:
219+
Ahora estás usando a una mezcla de exports por defecto y con nombre:
220220

221221
* `Gallery.js`:
222-
- Exports the `Profile` component as a **named export called `Profile`**.
223-
- Exports the `Gallery` component as a **default export**.
222+
- Exporta el componente `Profile` como un **export con nombre llamado `Profile`**.
223+
- Exporta el componente `Gallery` como un **export por defecto**.
224224
* `App.js`:
225-
- Imports `Profile` as a **named import called `Profile`** from `Gallery.js`.
226-
- Imports `Gallery` as a **default import** from `Gallery.js`.
227-
- Exports the root `App` component as a **default export**.
225+
- Importa `Profile` como un **import con nombre llamado `Profile`** desde `Gallery.js`.
226+
- Importa `Gallery` como un **import por defecto** desde `Gallery.js`.
227+
- Exporta el componente raíz `App` como un **export por defecto**.
228228

229229
<Recap>
230230

231-
On this page you learned:
231+
En esta página aprendiste:
232232

233-
* What a root component file is
234-
* How to import and export a component
235-
* When and how to use default and named imports and exports
236-
* How to export multiple components from the same file
233+
* Qué es un archivo de componente raíz
234+
* Como importar y exportar un componente
235+
* Cuándo y cómo usar imports y exports por defecto y con nombre
236+
* Cómo exportar múltiples componentes desde el mismo archivo
237237

238238
</Recap>
239239

240240

241241

242242
<Challenges>
243243

244-
### Split the components further {/*split-the-components-further*/}
244+
### Separa los componentes aún más {/*split-the-components-further*/}
245245

246-
Currently, `Gallery.js` exports both `Profile` and `Gallery`, which is a bit confusing.
246+
Actualmente, `Gallery.js` exporta tanto `Profile` como `Gallery`, lo cual es un poco confuso.
247247

248-
Move the `Profile` component to its own `Profile.js`, and then change the `App` component to render both `<Profile />` and `<Gallery />` one after another.
248+
Mueve el componente `Profile` a su propio `Profile.js`, y luego cambia el componente `App` para que renderice tanto `<Profile />` como `<Gallery />` uno detrás del otro.
249249

250-
You may use either a default or a named export for `Profile`, but make sure that you use the corresponding import syntax in both `App.js` and `Gallery.js`! You can refer to the table from the deep dive above:
250+
Puedes usar o bien un export por defecto o bien un export con nombre para `Profile`, ¡pero asegúrate de usar la sintaxis de import correspondiente tanto en `App.js` como en `Gallery.js`! Te puedes apoyar en la tabla de la sección de profundización de arriba:
251251

252-
| Syntax | Export statement | Import statement |
252+
| Sintaxis | Sentencia export | Sentencia import |
253253
| ----------- | ----------- | ----------- |
254-
| Default | `export default function Button() {}` | `import Button from './button.js';` |
255-
| Named | `export function Button() {}` | `import { Button } from './button.js';` |
254+
| Por defecto | `export default function Button() {}` | `import Button from './button.js';` |
255+
| Con nombre | `export function Button() {}` | `import { Button } from './button.js';` |
256256

257257
<Hint>
258258

259-
Don't forget to import your components where they are called. Doesn't `Gallery` use `Profile`, too?
259+
No te olvides de importar tus componentes cuando se llaman. ¿No usa `Gallery` también a `Profile`?
260260

261261
</Hint>
262262

@@ -307,11 +307,11 @@ img { margin: 0 10px 10px 0; height: 90px; }
307307

308308
</Sandpack>
309309

310-
After you get it working with one kind of exports, make it work with the other kind.
310+
Después de tenerlo funcionando con un tipo de export, hazlo funcionar con el otro tipo.
311311

312312
<Solution>
313313

314-
This is the solution with named exports:
314+
Esta es la solución con exports con nombre:
315315

316316
<Sandpack>
317317

@@ -361,7 +361,7 @@ img { margin: 0 10px 10px 0; height: 90px; }
361361

362362
</Sandpack>
363363

364-
This is the solution with default exports:
364+
Esta es la solución con exports por defecto:
365365

366366
<Sandpack>
367367

beta/src/sidebarLearn.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"path": "/learn/your-first-component"
4848
},
4949
{
50-
"title": "Importing and Exporting Components",
50+
"title": "Importar y exportar componentes",
5151
"path": "/learn/importing-and-exporting-components"
5252
},
5353
{

0 commit comments

Comments
 (0)