export multiple components from a file in react.js-ag捕鱼王app官网

current location:home > learning > web front-end > react >

export multiple components from a file in react.js

author:jiyik last updated:2025/03/06 views:

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 functionsmallbutton() {
  return <button>smallbutton>;
}

// 👇️ named export
export functionbigbutton() {
  return <buttonstyle={{padding: '20px40px'}}>bigbutton>;
}

请注意, using on the same line as a function definition exportis the same as exporting the component as an object after declaring it.

buttons.js

functionsmallbutton() {
  return <button>smallbutton>;
}

functionbigbutton() {
  return <buttonstyle={{padding: '20px40px'}}>bigbutton>;
}

// 👇️ 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 functionapp() {
  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/exportthe 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 functionsmallbutton() {
  return <button>smallbutton>;
}

const bigbutton = () => {
  return <buttonstyle={{padding: '20px40px'}}>bigbutton>;
}

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 functionsmallbutton() {
  return <button>smallbutton>;
}

// 👇️ named export
export const bigbutton = () => {
  return <buttonstyle={{padding: '20px40px'}}>bigbutton>;
};

here is how we will import these two components.

// 👇️ default and named imports
import smallbutton, {bigbutton} from './buttons';

export default functionapp() {
  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.

previous:

next: none

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.

article url:

related articles

publish date:2025/03/06 views:111 category:react

当我们将不是函数的值传递给元素的 onclick 属性时,会出现错误 expected onclick listener to be a function 。 要解决该错误,请确保仅将函数传递给元素的 onclick 属性。 const app = () = { // ⛔️

publish date:2025/03/06 views:111 category:react

在 react typescript 中将函数作为 props 传递: 在组件的接口中定义函数属性的类型。 在父组件中定义函数。 将函数作为道具传递给子组件。 interface buttonprops { sum : ( a: number , b: number ) =

publish date:2025/03/06 views:143 category:react

要使用react.js 创建一个仅包含允许的数字的输入字段: 将输入字段的类型设置为文本。 添加一个删除所有非数字值的 onchange 事件处理程序。 将输入值保存在状态变量中。 import {usest

publish date:2025/03/06 views:136 category:react

出现component cannot be used as a jsx component的错误有多种原因: 返回 jsx 元素数组而不是单个元素。 从组件返回 jsx 元素或 null 以外的任何值。 具有过时版本的 react 类型。 下面是一个产生上

publish date:2025/03/06 views:110 category:react

在 react 中检查输入值是否为有效数字: 检查输入的值是否不是空字符串或仅包含空格。 将值传递给 isnan() 函数。 如果 isnan 返回 false ,则该值为有效数字。 import {usestate} from react ; f

publish date:2025/03/06 views:185 category:react

在 react.js 中检测用户何时按下退格键: 将 onkeydown 道具添加到输入元素。 每次用户在输入字段中按下一个键时,检查按下的键是否为退格键。 调用函数或运行某些逻辑(如果是)。

publish date:2025/03/06 views:136 category:react

在 react 中使用内联样式设置背景颜色: 在元素上设置样式 style 属性。 将 backgroundcolor 属性设置为特定颜色。 import {usestate} from react ; export default function app () { const [isactive, setisactive] =

publish date:2025/03/06 views:168 category:react

react.js 错误 x is not defined react/jsx-no-undef 发生在我们忘记在代码中导入函数、类或变量之前使用它。 要解决该错误,请确保在我们的代码中使用之前导入该值,例如 import {myfunc} from my-

publish date:2025/03/06 views:132 category:react

要修复 react.js 警告invalid dom property for . did you mean tmlfor 。 使用 htmlfor 属性是因为 for 是 javascript 中的保留字。 下面是警告如何产生的示例。 export default function app () { // ⛔️ warning: inv

scan to read all tech tutorials

social media
  • https://www.github.com/onmpw
  • qq:1244347461

recommended

tags

scan the code
easier access tutorial
网站地图