React Native 导入或包含 JS 文件

React Native import or include JS file

如果我有超过 300 个功能组件文件需要导入到名为“main.js”的文件中,而不是像这样添加 300 行导入:

从“./pages/C1”导入 C1;

.

.

.

从“./pages/C300”导入 C300;

我想把这 300 个导入文件放在一个名为“myImports.js”的外部文件中,然后 include/import 这个“myImports.js”在“main.js”

或者你可以做的更好的方法是创建一个新文件夹,如 components 并将所有 300 个文件放入其中,并在那里创建新文件,名称如 index.js 并复制下面的代码,如

import C1 from "./pages/C1";
.
.
import C300 from "./pages/C300";

export {
  C1,C2,...C300
};

您的组件文件夹结构类似于

> components
    ----- C1.js
    ----- C2.js
    .
    .
    ----- C300.js
    ----- index.js // new file create here

用法喜欢

import { C300 } from "./components";