在 React-Native 中,如何根据我的环境文件选择一个资产文件夹而不是另一个文件夹?

In React-Native, how can I choose an assets folder instead of another one based on my environment file?

我正在构建白标应用程序。

我有一个资产文件夹,每个品牌包含一个图像文件夹和一个字体文件夹。

像这样:

assets/
   brand1/
      fonts/
      images/
        img1.png
        img2.png
        img3.png
   brand2/
      fonts/
      images/
        img1.png
        img2.png
        img3.png
src/

我的想法是在资产文件夹的根目录下有一个 index.js,我可以根据我的环境文件导出相关的文件夹集。

虽然我不确定如何实现。

有什么提示吗?

(我担心的是我不想在我的每个组件中进行环境调节)

让我们假设您有两个环境;

.env.brand1 NAME=BRAND1 .env.brand2 NAME=BRAND2

创建一个 index.js 文件,其代码应如下所示;

const images = {
    BRAND1: {
        logo: require('./brand1/brand1Logo.jpeg')
    },
    BRAND2: {
        logo: require('./brand2/brand2Logo.jpeg')
    }

}

export default images;

您现在可以像这样在任何文件中使用图像;

... other imports
import images from '#assets/index'

export default function () {
    
    ...other code 

    <Image source={images[Config.NAME].logo} />

}