导入(import)及导出(export)组件

您所在的位置:网站首页 su怎么导出组件 导入(import)及导出(export)组件

导入(import)及导出(export)组件

2023-07-19 12:53| 来源: 网络整理| 查看: 265

组件的魔力在于可重用性:你可以创建由其它组件组成的新组件。但是,当你嵌套越来越多的组件时,将各个组件拆分到单独的文件中会更有意义。这让你能够轻松地查找文件,并在更多的地方重用组件。

You will learn什么是根组件(root component)文件如何导入(import)及导出(export)组件何时使用When to use default and named imports and exports如何从一个文件中导入(import)和导出(export)多个组件如何将各个组件拆分到单独的文件中根组件(root component)文件

在 编写你的第一个组件 章节,你创建了一个 Profile 组件和一个 Gallery component 组件,并渲染它们:

Forkfunction Profile() { return ( Katherine Johnson ); } export default function Gallery() { return ( Amazing scientists ); }

To enter the code editing mode, press Enter. To exit the edit mode, press Escape

You are editing the code. To exit the edit mode, press Escape

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:

Forkimport Gallery from './Gallery.js'; export default function App() { return ( ); }

To enter the code editing mode, press Enter. To exit the edit mode, press Escape

You are editing the code. To exit the edit mode, press Escape

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.App.js:Imports Gallery as a default import from Gallery.js.Exports the root App component as a default export.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 DiveDefault vs Named Exports

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.

Default and named exports

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:

SyntaxExport statementImport statementDefaultexport default function Button() {}import Button from './button.js';Namedexport function Button() {}import { Button } from './button.js';

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!

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.

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 from the App component:

export default function App() { return ; }

Now Gallery.js contains two exports: a default Gallery export, and a named Profile export. App.js imports both of them. Try editing to and back in this example:

Forkimport Gallery from './Gallery.js'; import { Profile } from './Gallery.js'; export default function App() { return ( ); }

To enter the code editing mode, press Enter. To exit the edit mode, press Escape

You are editing the code. To exit the edit mode, press Escape

Now you’re using a mix of default and named exports:

Gallery.js:Exports the Profile component as a named export called Profile.Exports the Gallery component as a default export.App.js:Imports Profile as a named import called Profile from Gallery.js.Imports Gallery as a default import from Gallery.js.Exports the root App component as a default export.Recap

On this page you learned:

What a root component file isHow to import and export a componentWhen and how to use default and named imports and exportsHow to export multiple components from the same fileTry out some challengesChallenge 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 and 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:

SyntaxExport statementImport statementDefaultexport default function Button() {}import Button from './button.js';Namedexport function Button() {}import { Button } from './button.js';Fork// Move me to Profile.js! export function Profile() { return ( Alan L. Hart ); } export default function Gallery() { return ( Amazing scientists ); }

To enter the code editing mode, press Enter. To exit the edit mode, press Escape

You are editing the code. To exit the edit mode, press Escape

After you get it working with one kind of exports, make it work with the other kind.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3