简体   繁体   English

在React中导出组件时如何避免使用默认导出?

[英]How can I avoid using default export when exporting a component in React?

I have a the following component as a function: 我有以下组件作为功能:

function Modal(props) {}

I export it this way: 我这样导出:

export default Modal;

However, I want to export it form its index.js file in this way: 但是,我想以这种方式将其从index.js文件导出:

export { Modal } from "./Modal";

But, I get an error... does anyone know why? 但是,我得到一个错误...有人知道为什么吗?

You can also export it like this 您也可以这样导出

export function Model(props) {}

and import it like this 然后像这样导入

import {Model} from './Model';

export { default as Modal } from "./Modal";

The problem is that you are trying to import a default exported component as a named exported one. 问题是您试图将默认的导出组件导入为命名的导出组件。

import Modal from './Modal'; // that's how we import a "default exported" component
import { Modal } from './Modal' // that's how we import a "named exported" component

To achieve what you want, you could either change the export of your main component to be a named export. 要实现所需的功能,可以将主要组件的导出更改为命名导出。 Or you could import it like that in your index.js file: 或者,您也可以将其导入到index.js文件中:

import Modal from './Modal';

export { Modal };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导出时,React.Component是默认扩展名吗? - Is React.Component the default extension when exporting? 当参数组件使用反应路由器在 ReactJS 中渲染时,如何避免不必要的 HOC 组件重新渲染 - How can I avoid unnecessary re rendering of an HOC component, when the parameter component renders in ReactJS with react router 我如何使用导出导出material -ui组件 - How can I export a react material-ui component with 如何从reacjs中的默认导出模块导出 - How can export from default-exporting module in reacjs 在使用带有TypeScript的redux时,如何在没有ownProps的情况下导出React组件? - How to export a React Component without ownProps when using redux with TypeScript? 如何使用Flow键入默认导出? - How can I type a default export using Flow? 在与Backbone一起使用React时,我可以避免使用forceUpdate()吗? - Can I avoid forceUpdate() when using React with Backbone? 如何使用功能组件导出 React 图标? - How to Export React Icons Using Functional Component? 我该如何解决这个错误? “在'./App'中找不到导出'默认'(导入为'App')(可能的导出:App)”(我使用反应) - How can I solve this error? "export 'default' (imported as 'App') was not found in './App' (possible exports: App)" (im using react) 即使在 ReactJS 中导出组件后,默认导出错误 - Default export error even after exporting the component in ReactJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM