Export multiple components from a file in React.js
Exporting multiple components in React using named exports like export function A() {}
and export function B() {}
. The exported components can be imported using named imports like import {A, B} from './another-file'
. We can have as many named exports as we need in a single file.
Below is an example of exporting multiple components from a file called Buttons.js .
Buttons.js
// 👇️ named export export function SmallButton() { return <button>Small</button>; } // 👇️ named export export function BigButton() { return <button style={{padding: '20px 40px'}}>Big</button>; }
请注意
, using on the same line as a function definitionexport
is the same as exporting the component as an object after declaring it.
Buttons.js
function SmallButton() { return <button>Small</button>; } function BigButton() { return <button style={{padding: '20px 40px'}}>Big</button>; } // 👇️ named exports export {SmallButton, BigButton};
Either of these two approaches can be used when exporting a class component, export class A{}
e.g.
Here is how we import the component in a file called App.js.
App.js
// 👇️ named imports import {SmallButton, BigButton} from './Buttons'; export default function App() { return ( <div> <SmallButton /> <BigButton /> </div> ); }
Make sure to correct the path to the Buttons.js module if you have to. The example above assumes that Buttons.js and App.js are in the same directory.
For example, if we are importing from one directory up, we can import {SmallButton, BigButton} from '../Buttons'
.
We import function components by enclosing their names in curly braces — this is called a named import.
import/export
The syntax is called ES6 modules in JavaScript.
To be able to import a component from a different file, it must be exported using a named or default export.
The examples above use named exports and named imports.
The main difference between named and default exports and imports is - you can have multiple named exports per file, but you can have only one default export.
If we try to use multiple default exports in a single file, we will get an error.
Buttons.js
// ⛔️ Only one default export allowed per module. export default function SmallButton() { return <button>Small</button>; } const BigButton = () => { return <button style={{padding: '20px 40px'}}>Big</button>; } export default BigButton;
重要提示
: If we export a variable (or arrow function) as default export, we must declare it on the first line and export it on the next line. You cannot declare and default export a variable on the same line.
Having said that, we can use 1 default export and any number of named exports in a single file.
Let's look at an example of exporting multiple components and using both default and named exports.
Buttons.js
// 👇️ default export export default function SmallButton() { return <button>Small</button>; } // 👇️ named export export const BigButton = () => { return <button style={{padding: '20px 40px'}}>Big</button>; };
Here is how we will import these two components.
// 👇️ default and named imports
import SmallButton, {BigButton} from './Buttons';
export default function App() {
return (
<div>
<SmallButton />
<BigButton />
</div>
);
}
请注意
, we did not enclose the default import in curly braces.
We import the SmallButton component using the default import and the BigButton component using a named import.
请注意
, each file can have only one default export, but we can have as many named exports as needed.
In my experience, most real-world codebases use named exports and imports exclusively, as they make it easier to take advantage of IDE auto-completion and auto-import.
We also don't have to think about which members to export using default or named exports.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to avoid cross-origin (CORS) issues in React/Next.js
Publish Date:2025/03/17 Views:166 Category:NETWORK
-
In this article, we will introduce how to avoid cross-origin (CORS) issues in React/Next.js. Cross-origin resource sharing (CORS) is a protocol that defines how web requests should be handled when crossing different URLs.
React Tutorial - Transferring Props
Publish Date:2025/03/16 Views:185 Category:React
-
React transfers Props. Props are generated when components are encapsulated. Components expose some properties (Props) to the outside world to complete some functions.
React Tutorial: Props Anti-Pattern
Publish Date:2025/03/16 Views:183 Category:React
-
React's Props anti-pattern, using Props to generate state in getInitialState is an anti-pattern - Anti-Pattern.
React Tutorial - Props Validation
Publish Date:2025/03/16 Views:99 Category:React
-
Props validation is a very useful way to use components correctly. It can avoid many bugs and problems as your application becomes more and more complex. In addition, it can make your program more readable.
React tutorial: Types of Props for child components
Publish Date:2025/03/16 Views:170 Category:React
-
Usually, the child components of a React component are a group, that is, the child components are an array. Introduction to Type of the Children Props.
How to solve the error Uncaught TypeError: Cannot read properties of undefined in
Publish Date:2025/03/16 Views:150 Category:React
-
In the process of React development, we often encounter some errors. Here we look at an error reported in App.js. The error is as follows: App.js:69 Uncaught TypeError: Cannot read properties of undefined (reading 'setState') at onInput
Why do you need to bind event handlers in React Class Components?
Publish Date:2025/03/16 Views:58 Category:React
-
When using React, we must have come across control components and event handlers. We need to use `.bind()` in the constructor of the custom component to bind these methods to the component instance. As shown in the following code:
Solution to the error "does not contain a default export" in React
Publish Date:2025/03/16 Views:187 Category:React
-
When we try to use `default import` to import from a module that does not have a `default export`, we get a "does not contain a default export" error. To fix the error, make sure the module has named exports and wrap the import in curly braces, e.g.
Error in React: Attempted import error 'X' is not exported from Solution
Publish Date:2025/03/16 Views:76 Category:React
-
In React, the error “Attempted import error 'X' is not exported from” in React.js occurs when we try to import a named import that does not exist in the specified file. To fix the error, make sure the module has named exports and you have not obfu