在 React 中从另一个文件导入组件
Import Components from another file in React
在 React 中从另一个文件导入组件:
- 从文件导出组件
A
,例如export function Button() {}
。 - 将文件中的组件导入
B
为import {Button} from './another-file'
. - 在文件中使用导入的组件
B
。
下面是从名为another-file.js
.
// 👇️ named export export function BigButton() { return ( <button style={{padding: '2rem 1rem'}} onClick={() => console.log('big button')} > Big button </button> ); } // 👇️ named export export const SmallButton = () => { return ( <button onClick={() => console.log('small button')}>Small button</button> ); };
以下是我们如何将组件导入名为App.js
.
// 👇️ named import import {BigButton, SmallButton} from './another-file'; export default function App() { return ( <div> <BigButton /> <hr /> <SmallButton /> </div> ); }
如果必须,请确保更正指向another-file.js
模块的路径。上面的示例假定another-file.js
和App.js
位于同一目录中。
例如,如果another-file.js
位于上一级目录,则必须导入为import {BigButton} from '../another-file'
.
导入/导出语法称为
JavaScript 模块。
上面的示例使用命名导出和命名导入。
命名和默认导出和导入之间的主要区别是 – 每个文件可以有多个命名导出,但只能有一个默认导出。
Let’s look at an example of how we would import a component that was exported
using a default export.
// 👇️ default export export default function BigButton() { return ( <button style={{padding: '2rem 1rem'}} onClick={() => console.log('big button')} > Big button </button> ); }
IMPORTANT: If you are exporting a variable (or an arrow function) as a
default export, you have to declare it on 1 line and export it on the next. You
can’t declare and default export a variable on the same line.
const BigButton = () => { return ( <button style={{padding: '2rem 1rem'}} onClick={() => console.log('big button')} > Big button </button> ); } // 👇️ default export export default BigButton;
And here is how we would import the component using a default import.
// 👇️ default import import BigButton from './another-file'; export default function App() { return ( <div> <BigButton /> </div> ); }
We could have also used a different name when importing the component, e.g.
Foo
.
// 👇️ default import import Foo from './another-file'; export default function App() { return ( <div> <Foo /> </div> ); }
This works, but is confusing and should be avoided.
You also don’t have to think about which members are exported with a default or named export.
You can also mix and match, here is an example of a file that uses both – a
default and a named export.
// 👇️ default export export default function BigButton() { return ( <button style={{padding: '2rem 1rem'}} onClick={() => console.log('big button')} > Big button </button> ); } // 👇️ named export export const SmallButton = () => { return ( <button onClick={() => console.log('small button')}>Small button</button> ); };
And here is how you would import the two components.
// 👇️ default and named imports import BigButton, {SmallButton} from './another-file'; export default function App() { return ( <div> <BigButton /> <hr /> <SmallButton /> </div> ); }
We used a default import to import the BigButton
component and a named import
to import the SmallButton
component.
Note that you can only have a single default export per file, but you can have
as many named exports as necessary.