简体   繁体   English

如何在 React Native 中使用“import * as {name}”语法导入组件?

[英]How can components be imported with "import * as {name}" syntax in react native?

In a react-native project, I would prefer to be able to import all my component like I do with types in my reducers.在 react-native 项目中,我希望能够像在 reducer 中导入类型一样导入所有组件。

I want to be able to write: import * as Components from '../components'我希望能够写: import * as Components from '../components'

so I went to my components folder, created an index.js file, imported all the basic components, and exported them like export const ComponentExample1 = ComponentExample1 & export const ComponentExample1 = <ComponentExample/> .所以我去了我的 components 文件夹,创建了一个 index.js 文件,导入了所有基本组件,然后像export const ComponentExample1 = ComponentExample1 & export const ComponentExample1 = <ComponentExample/>一样导出它们。 I figured there might be some naming related errors and thats what seems to have happened because I get the error:我认为可能存在一些与命名相关的错误,这似乎是因为我收到错误消息:

Error: TransformErro SyntaxError: ......index.js: Identifier "ComponentExample1" has already been declared

All of my basic components are exported intra-component as export default ComponentExample1我所有的基本组件都在组件内导出为export default ComponentExample1

How can I change my approach to effect my end desire?我怎样才能改变我的方法来实现我的最终愿望?

You can re-export your default exported components in index.js您可以在 index.js 中重新导出默认导出的组件

Example:例子:

export { default as RoundButton } from './RoundButton'
export { default as Logo } from './Logo'

删除export const ComponentExample1 = ComponentExample1并只保留export const ComponentExample1 = <ComponentExample />

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM