我正在React
中创建一个项目,但不确定导入/导出的方法是否正确。
我正在使用我的Components文件夹中的index.js
文件导出组件,如下所示:
export {default as Card} from './Card/Card'
export {default as CardList} from './CardList/CardList'
每个组件都是一个const,我将其导出为:
export default Card
和export default CardList
我两次使用
export default
在
index.js
在每个组件文件中
这是使用索引文件导出组件的最佳实践吗?
谢谢!
最佳答案
您可以继续将组件导出为默认组件,并且在index.js中,可以像下面这样导出它们:
export Card from './Card/Card';
export CardList from './CardList/CardList';
然后使用这些时,您可以执行以下操作:
import {Card, CardList} from 'components';
您的联系点(index.js)不太冗长
关于javascript - React:导入/导出的最佳方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49291321/