[英]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.