You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -42,17 +47,14 @@ export default function Gallery() {
42
47
```
43
48
44
49
```css
45
-
img {
46
-
margin: 010px10px0;
47
-
height: 90px;
48
-
}
50
+
img { margin: 010px10px0; height: 90px; }
49
51
```
50
52
51
53
</Sandpack>
52
54
53
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.
54
56
55
-
## Exporting and importing a component {/_exporting-and-importing-a-component_/}
57
+
## Exporting and importing a component {/*exporting-and-importing-a-component*/}
56
58
57
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:
58
60
@@ -68,13 +70,20 @@ Here both `Profile` and `Gallery` have been moved out of `App.js` into a new fil
68
70
importGalleryfrom'./Gallery.js';
69
71
70
72
exportdefaultfunctionApp() {
71
-
return<Gallery />;
73
+
return (
74
+
<Gallery />
75
+
);
72
76
}
73
77
```
74
78
75
79
```js Gallery.js
76
80
functionProfile() {
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
+
);
78
87
}
79
88
80
89
exportdefaultfunctionGallery() {
@@ -90,28 +99,26 @@ export default function Gallery() {
90
99
```
91
100
92
101
```css
93
-
img {
94
-
margin: 010px10px0;
95
-
height: 90px;
96
-
}
102
+
img { margin: 010px10px0; height: 90px; }
97
103
```
98
104
99
105
</Sandpack>
100
106
101
107
Notice how this example is broken down into two component files now:
102
108
103
109
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**.
106
112
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
+
109
116
110
117
<Note>
111
118
112
119
You may encounter files that leave off the `.js` file extension like so:
113
120
114
-
```js
121
+
```js
115
122
importGalleryfrom'./Gallery';
116
123
```
117
124
@@ -127,20 +134,20 @@ There are two primary ways to export values with JavaScript: default exports and
127
134
128
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:
| 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';`|
134
141
135
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!
136
143
137
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.
138
145
139
146
</DeepDive>
140
147
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*/}
142
149
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!**
144
151
145
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!
146
153
@@ -155,7 +162,7 @@ export function Profile() {
155
162
Then, **import**`Profile` from `Gallery.js` to `App.js` using a named import (with the curly braces):
156
163
157
164
```js
158
-
import {Profile} from'./Gallery.js';
165
+
import {Profile} from'./Gallery.js';
159
166
```
160
167
161
168
Finally, **render**`<Profile />` from the `App` component:
@@ -172,16 +179,23 @@ Now `Gallery.js` contains two exports: a default `Gallery` export, and a named `
172
179
173
180
```js App.js
174
181
importGalleryfrom'./Gallery.js';
175
-
import {Profile} from'./Gallery.js';
182
+
import {Profile} from'./Gallery.js';
176
183
177
184
exportdefaultfunctionApp() {
178
-
return<Profile />;
185
+
return (
186
+
<Profile />
187
+
);
179
188
}
180
189
```
181
190
182
191
```js Gallery.js
183
192
exportfunctionProfile() {
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
+
);
185
199
}
186
200
187
201
exportdefaultfunctionGallery() {
@@ -197,20 +211,17 @@ export default function Gallery() {
197
211
```
198
212
199
213
```css
200
-
img {
201
-
margin: 010px10px0;
202
-
height: 90px;
203
-
}
214
+
img { margin: 010px10px0; height: 90px; }
204
215
```
205
216
206
217
</Sandpack>
207
218
208
219
Now you're using a mix of default and named exports:
209
220
210
-
-`Gallery.js`:
221
+
*`Gallery.js`:
211
222
- Exports the `Profile` component as a **named export called `Profile`**.
212
223
- Exports the `Gallery` component as a **default export**.
213
-
-`App.js`:
224
+
*`App.js`:
214
225
- Imports `Profile` as a **named import called `Profile`** from `Gallery.js`.
215
226
- Imports `Gallery` as a **default import** from `Gallery.js`.
216
227
- Exports the root `App` component as a **default export**.
@@ -219,27 +230,29 @@ Now you're using a mix of default and named exports:
219
230
220
231
On this page you learned:
221
232
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
226
237
227
238
</Recap>
228
239
240
+
241
+
229
242
<Challenges>
230
243
231
-
### Split the components further {/_split-the-components-further_/}
244
+
### Split the components further {/*split-the-components-further*/}
232
245
233
246
Currently, `Gallery.js` exports both `Profile` and `Gallery`, which is a bit confusing.
234
247
235
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.
236
249
237
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:
0 commit comments