导入(import)及导出(export)组件
组件的魔力在于可重用性:你可以创建由其它组件组成的新组件。但是,当你嵌套越来越多的组件时,将各个组件拆分到单独的文件中会更有意义。这让你能够轻松地查找文件,并在更多的地方重用组件。
You will learn
- 什么是根组件(root component)文件
- 如何导入(import)及导出(export)组件
- 何时使用When to use default and named imports and exports
- 如何从一个文件中导入(import)和导出(export)多个组件
- 如何将各个组件拆分到单独的文件中
根组件(root component)文件
在 编写你的第一个组件 章节,你创建了一个 Profile
组件和一个 Gallery
component 组件,并渲染它们:
function Profile() { return ( <img src="https://i.imgur.com/MK3eW3As.jpg" alt="Katherine Johnson" /> ); } export default function Gallery() { return ( <section> <h1>Amazing scientists</h1> <Profile /> <Profile /> <Profile /> </section> ); }
These currently live in a root component file, named App.js
in this example. In Create React App, 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.
导出(export)及导入(import)组件
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:
- Make a new JS file to put the components in.
- Export your function component from that file (using either default or named exports).
- Import it in the file where you’ll use the component (using the corresponding technique for importing default or named exports).
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
:
import Gallery from './Gallery.js'; export default function App() { return ( <Gallery /> ); }
Notice how this example is broken down into two component files now:
Gallery.js
:- Defines the
Profile
component which is only used within the same file and is not exported. - Exports the
Gallery
component as a default export.
- Defines the
App.js
:- Imports
Gallery
as a default import fromGallery.js
. - Exports the root
App
component as a default export.
- Imports
Note
You may encounter files that leave off the .js
file extension like so:
import Gallery from './Gallery';
Either './Gallery.js'
or './Gallery'
will work with React, though the former is closer to how native ES Modules work.
Deep Dive
Default vs Named Exports
Exporting and importing multiple components from the same file
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!
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!
First, export Profile
from Gallery.js
using a named export (no default
keyword):
export function Profile() {
// ...
}
Then, import Profile
from Gallery.js
to App.js
using a named import (with the curly braces):
import { Profile } from './Gallery.js';
Finally, render <Profile />
from the App
component:
export default function App() {
return <Profile />;
}
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:
import Gallery from './Gallery.js'; import { Profile } from './Gallery.js'; export default function App() { return ( <Profile /> ); }
Now you’re using a mix of default and named exports:
Gallery.js
:- Exports the
Profile
component as a named export calledProfile
. - Exports the
Gallery
component as a default export.
- Exports the
App.js
:- Imports
Profile
as a named import calledProfile
fromGallery.js
. - Imports
Gallery
as a default import fromGallery.js
. - Exports the root
App
component as a default export.
- Imports
Recap
On this page you learned:
- What a root component file is
- How to import and export a component
- When and how to use default and named imports and exports
- How to export multiple components from the same file
Try out some challenges
Challenge 1 of 1: Split the components further
Currently, Gallery.js
exports both Profile
and Gallery
, which is a bit confusing.
Move the Profile
component to its own Profile.js
, and then change the App
component to render both <Profile />
and <Gallery />
one after another.
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:
Syntax | Export statement | Import statement |
---|---|---|
Default | export default function Button() {} | import Button from './button.js'; |
Named | export function Button() {} | import { Button } from './button.js'; |
// Move me to Profile.js! export function Profile() { return ( <img src="https://i.imgur.com/QIrZWGIs.jpg" alt="Alan L. Hart" /> ); } export default function Gallery() { return ( <section> <h1>Amazing scientists</h1> <Profile /> <Profile /> <Profile /> </section> ); }
After you get it working with one kind of exports, make it work with the other kind.