Skip to content

Commit f1ebee4

Browse files
committed
Revert formatting in "importing and exporting components"
1 parent 1ae2598 commit f1ebee4

File tree

1 file changed

+137
-62
lines changed

1 file changed

+137
-62
lines changed

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

Lines changed: 137 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,28 @@ The magic of components lies in their reusability: you can create components tha
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+
* 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
1818

1919
</YouWillLearn>
2020

21-
## The root component file {/_the-root-component-file_/}
21+
## The root component file {/*the-root-component-file*/}
2222

2323
In [Twój pierwszy komponent](/learn/your-first-component), you made a `Profile` component and a `Gallery` component that renders it:
2424

2525
<Sandpack>
2626

2727
```js
2828
function Profile() {
29-
return <img src="https://i.imgur.com/MK3eW3As.jpg" alt="Katherine Johnson" />;
29+
return (
30+
<img
31+
src="https://i.imgur.com/MK3eW3As.jpg"
32+
alt="Katherine Johnson"
33+
/>
34+
);
3035
}
3136

3237
export default function Gallery() {
@@ -42,17 +47,14 @@ export default function Gallery() {
4247
```
4348

4449
```css
45-
img {
46-
margin: 0 10px 10px 0;
47-
height: 90px;
48-
}
50+
img { margin: 0 10px 10px 0; height: 90px; }
4951
```
5052

5153
</Sandpack>
5254

5355
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.
5456

55-
## Exporting and importing a component {/_exporting-and-importing-a-component_/}
57+
## Exporting and importing a component {/*exporting-and-importing-a-component*/}
5658

5759
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:
5860

@@ -68,13 +70,20 @@ Here both `Profile` and `Gallery` have been moved out of `App.js` into a new fil
6870
import Gallery from './Gallery.js';
6971

7072
export default function App() {
71-
return <Gallery />;
73+
return (
74+
<Gallery />
75+
);
7276
}
7377
```
7478

7579
```js Gallery.js
7680
function Profile() {
77-
return <img src="https://i.imgur.com/QIrZWGIs.jpg" alt="Alan L. Hart" />;
81+
return (
82+
<img
83+
src="https://i.imgur.com/QIrZWGIs.jpg"
84+
alt="Alan L. Hart"
85+
/>
86+
);
7887
}
7988

8089
export default function Gallery() {
@@ -90,28 +99,26 @@ export default function Gallery() {
9099
```
91100

92101
```css
93-
img {
94-
margin: 0 10px 10px 0;
95-
height: 90px;
96-
}
102+
img { margin: 0 10px 10px 0; height: 90px; }
97103
```
98104

99105
</Sandpack>
100106

101107
Notice how this example is broken down into two component files now:
102108

103109
1. `Gallery.js`:
104-
- Defines the `Profile` component which is only used within the same file and is not exported.
105-
- Exports the `Gallery` component as a **default export**.
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**.
106112
2. `App.js`:
107-
- Imports `Gallery` as a **default import** from `Gallery.js`.
108-
- Exports the root `App` component as a **default export**.
113+
- Imports `Gallery` as a **default import** from `Gallery.js`.
114+
- Exports the root `App` component as a **default export**.
115+
109116

110117
<Note>
111118

112119
You may encounter files that leave off the `.js` file extension like so:
113120

114-
```js
121+
```js
115122
import Gallery from './Gallery';
116123
```
117124

@@ -127,20 +134,20 @@ There are two primary ways to export values with JavaScript: default exports and
127134

128135
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:
129136

130-
| Syntax | Export statement | Import statement |
131-
| ------- | ------------------------------------- | --------------------------------------- |
132-
| Default | `export default function Button() {}` | `import Button from './button.js';` |
133-
| Named | `export function Button() {}` | `import { Button } from './button.js';` |
137+
| Syntax | Export statement | Import statement |
138+
| ----------- | ----------- | ----------- |
139+
| Default | `export default function Button() {}` | `import Button from './button.js';` |
140+
| Named | `export function Button() {}` | `import { Button } from './button.js';` |
134141

135142
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!
136143

137144
**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.
138145

139146
</DeepDive>
140147

141-
## Exporting and importing multiple components from the same file {/_exporting-and-importing-multiple-components-from-the-same-file_/}
148+
## Exporting and importing multiple components from the same file {/*exporting-and-importing-multiple-components-from-the-same-file*/}
142149

143-
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+
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!**
144151

145152
> 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!
146153
@@ -155,7 +162,7 @@ export function Profile() {
155162
Then, **import** `Profile` from `Gallery.js` to `App.js` using a named import (with the curly braces):
156163

157164
```js
158-
import {Profile} from './Gallery.js';
165+
import { Profile } from './Gallery.js';
159166
```
160167

161168
Finally, **render** `<Profile />` from the `App` component:
@@ -172,16 +179,23 @@ Now `Gallery.js` contains two exports: a default `Gallery` export, and a named `
172179

173180
```js App.js
174181
import Gallery from './Gallery.js';
175-
import {Profile} from './Gallery.js';
182+
import { Profile } from './Gallery.js';
176183

177184
export default function App() {
178-
return <Profile />;
185+
return (
186+
<Profile />
187+
);
179188
}
180189
```
181190

182191
```js Gallery.js
183192
export function Profile() {
184-
return <img src="https://i.imgur.com/QIrZWGIs.jpg" alt="Alan L. Hart" />;
193+
return (
194+
<img
195+
src="https://i.imgur.com/QIrZWGIs.jpg"
196+
alt="Alan L. Hart"
197+
/>
198+
);
185199
}
186200

187201
export default function Gallery() {
@@ -197,20 +211,17 @@ export default function Gallery() {
197211
```
198212

199213
```css
200-
img {
201-
margin: 0 10px 10px 0;
202-
height: 90px;
203-
}
214+
img { margin: 0 10px 10px 0; height: 90px; }
204215
```
205216

206217
</Sandpack>
207218

208219
Now you're using a mix of default and named exports:
209220

210-
- `Gallery.js`:
221+
* `Gallery.js`:
211222
- Exports the `Profile` component as a **named export called `Profile`**.
212223
- Exports the `Gallery` component as a **default export**.
213-
- `App.js`:
224+
* `App.js`:
214225
- Imports `Profile` as a **named import called `Profile`** from `Gallery.js`.
215226
- Imports `Gallery` as a **default import** from `Gallery.js`.
216227
- Exports the root `App` component as a **default export**.
@@ -219,27 +230,29 @@ Now you're using a mix of default and named exports:
219230

220231
On this page you learned:
221232

222-
- What a root component file is
223-
- How to import and export a component
224-
- When and how to use default and named imports and exports
225-
- How to export multiple components from the same file
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
226237

227238
</Recap>
228239

240+
241+
229242
<Challenges>
230243

231-
### Split the components further {/_split-the-components-further_/}
244+
### Split the components further {/*split-the-components-further*/}
232245

233246
Currently, `Gallery.js` exports both `Profile` and `Gallery`, which is a bit confusing.
234247

235248
Move the `Profile` component to its own `Profile.js`, and then change the `App` component to render both `<Profile />` and `<Gallery />` one after another.
236249

237250
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:
238251

239-
| Syntax | Export statement | Import statement |
240-
| ------- | ------------------------------------- | --------------------------------------- |
241-
| Default | `export default function Button() {}` | `import Button from './button.js';` |
242-
| Named | `export function Button() {}` | `import { Button } from './button.js';` |
252+
| Syntax | Export statement | Import statement |
253+
| ----------- | ----------- | ----------- |
254+
| Default | `export default function Button() {}` | `import Button from './button.js';` |
255+
| Named | `export function Button() {}` | `import { Button } from './button.js';` |
243256

244257
<Hint>
245258

@@ -251,7 +264,7 @@ Don't forget to import your components where they are called. Doesn't `Gallery`
251264

252265
```js App.js
253266
import Gallery from './Gallery.js';
254-
import {Profile} from './Gallery.js';
267+
import { Profile } from './Gallery.js';
255268

256269
export default function App() {
257270
return (
@@ -265,7 +278,12 @@ export default function App() {
265278
```js Gallery.js active
266279
// Move me to Profile.js!
267280
export function Profile() {
268-
return <img src="https://i.imgur.com/QIrZWGIs.jpg" alt="Alan L. Hart" />;
281+
return (
282+
<img
283+
src="https://i.imgur.com/QIrZWGIs.jpg"
284+
alt="Alan L. Hart"
285+
/>
286+
);
269287
}
270288

271289
export default function Gallery() {
@@ -281,14 +299,10 @@ export default function Gallery() {
281299
```
282300

283301
```js Profile.js
284-
285302
```
286303

287304
```css
288-
img {
289-
margin: 0 10px 10px 0;
290-
height: 90px;
291-
}
305+
img { margin: 0 10px 10px 0; height: 90px; }
292306
```
293307

294308
</Sandpack>
@@ -303,7 +317,7 @@ This is the solution with named exports:
303317

304318
```js App.js
305319
import Gallery from './Gallery.js';
306-
import {Profile} from './Profile.js';
320+
import { Profile } from './Profile.js';
307321

308322
export default function App() {
309323
return (
@@ -316,7 +330,7 @@ export default function App() {
316330
```
317331

318332
```js Gallery.js
319-
import {Profile} from './Profile.js';
333+
import { Profile } from './Profile.js';
320334

321335
export default function Gallery() {
322336
return (
@@ -332,15 +346,17 @@ export default function Gallery() {
332346

333347
```js Profile.js
334348
export function Profile() {
335-
return <img src="https://i.imgur.com/QIrZWGIs.jpg" alt="Alan L. Hart" />;
349+
return (
350+
<img
351+
src="https://i.imgur.com/QIrZWGIs.jpg"
352+
alt="Alan L. Hart"
353+
/>
354+
);
336355
}
337356
```
338357

339358
```css
340-
img {
341-
margin: 0 10px 10px 0;
342-
height: 90px;
343-
}
359+
img { margin: 0 10px 10px 0; height: 90px; }
344360
```
345361

346362
</Sandpack>
@@ -378,6 +394,65 @@ export default function Gallery() {
378394
}
379395
```
380396

397+
```js Profile.js
398+
export default function Profile() {
399+
return (
400+
<img
401+
src="https://i.imgur.com/QIrZWGIs.jpg"
402+
alt="Alan L. Hart"
403+
/>
404+
);
405+
}
406+
```
407+
408+
```css
409+
img { margin: 0 10px 10px 0; height: 90px; }
410+
```
411+
412+
</Sandpack>
413+
414+
</Solution>
415+
416+
</Challenges> (
417+
<section>
418+
<h1>Amazing scientists</h1>
419+
<Profile />
420+
<Profile />
421+
<Profile />
422+
</section>
423+
);
424+
}
425+
```
426+
427+
```js Profile.js
428+
export default function Profile() {
429+
return <img src="https://i.imgur.com/QIrZWGIs.jpg" alt="Alan L. Hart" />;
430+
}
431+
```
432+
433+
```css
434+
img {
435+
margin: 0 10px 10px 0;
436+
height: 90px;
437+
}
438+
```
439+
440+
</Sandpack>
441+
442+
</Solution>
443+
444+
</Challenges>
445+
(
446+
<section>
447+
<h1>Amazing scientists</h1>
448+
<Profile />
449+
<Profile />
450+
<Profile />
451+
</section>
452+
);
453+
}
454+
```
455+
381456
```js Profile.js
382457
export default function Profile() {
383458
return <img src="https://i.imgur.com/QIrZWGIs.jpg" alt="Alan L. Hart" />;

0 commit comments

Comments
 (0)