React native require images breaks with variables

前端 未结 1 702
予麋鹿
予麋鹿 2021-01-24 04:11

How come this line works with no errors

var gicon = species[ii].color[0] ? require(\'../assets/gLight.jpg\') : require(\'../assets/nLight.png\');
1条回答
  •  醉梦人生
    2021-01-24 04:48

    Image names are resolved during packaging. There is a section about it in the docs. You can solve your problem by defining constants for the images:

    const LIGHT_G = require('../assets/gLight.jpg');
    const LIGHT_N = require('../assets/nLight.png');
    
    which_light = LIGHT_G;
    var gicon = species[ii].color[0] ? which_light : LIGHT_N;
    

    You have to reference all possible images like this.

    0 讨论(0)
提交回复
热议问题