这是我的解决方案。
设置
文件结构:
app  
  |--src
    |--assets
      |--images
        |--logos
          |--small_kl_logo.png
          |--small_a1_logo.png
          |--small_kc_logo.png
          |--small_nv_logo.png
          |--small_other_logo.png
        |--index.js
    |--SearchableList.js
在index.js,我有这个:
const images = {
  logos: {
    kl: require('./logos/small_kl_logo.png'),
    a1: require('./logos/small_a1_logo.png'),
    kc: require('./logos/small_kc_logo.png'),
    nv: require('./logos/small_nv_logo.png'),
    other: require('./logos/small_other_logo.png'),
  }
};
export default images;
在我的SearchableList.js组件中,我然后像这样导入了 Images 组件:
import Images from './assets/images';
然后imageSelect我在我的组件中创建了一个新函数:
imageSelect = network => {
  if (network === null) {
    return Images.logos.other;
  }
  const networkArray = {
    'KL': Images.logos.kl,
    'A1': Images.logos.a1,
    'KC': Images.logos.kc,
    'NV': Images.logos.nv,
    'Other': Images.logos.other,
  };
  return networkArray[network];
};
然后在我的组件render函数中,我调用这个新imageSelect函数来根据 中的值动态分配所需的图像this.state.network:
render() {
  <Image source={this.imageSelect(this.state.network)} />
}
传递给 imageSelect 函数的值可以是任何动态字符串。我只是选择先将它设置在 state 中,然后传入。
我希望这个答案有帮助。:)