react native require not working for image source
<p>Given below code, react native complains </p>

<pre>
Requiring unknown module "../../images/Foo.png".If you are sure the module is there, try restarting the packager or running "npm install".
</pre>

<p>But the same code works fine if I hardcode the image source path in <code>require</code> call.</p>

<p><strong>Code not working</strong>: has a method call to determine icon name. Notice the <code>require('../../images/'+imageIconNameFor(prediction.type)</code> line.</p>

<pre><code>const PredictionItem = ({prediction}) =&gt; (
&lt;TouchableHighlight onPress={() =&gt; itemPressed()}&gt;
    &lt;View style={styles.predictionItem}&gt;
        &lt;Image style={styles.predictionIcon} source={require('../../images/'+imageIconNameFor(prediction.type))}&gt;&lt;/Image&gt;
    &lt;/View&gt;
&lt;/TouchableHighlight&gt;
);
function imageIconNameFor(predictionType) {
    return "Foo.png";
}
</code></pre>

<p><strong>Code working</strong>: Instead of calling a method to determine icon name, if I hardcode the icon name in <code>require</code> call, it works fine. Notice the <code>require('../../images/Foo.png')</code> line.</p>

<pre><code>const PredictionItem = ({prediction}) =&gt; (
&lt;TouchableHighlight onPress={() =&gt; itemPressed()}&gt;
    &lt;View style={styles.predictionItem}&gt;
        &lt;Image style={styles.predictionIcon} source={require('../../images/Foo.png')}&gt;&lt;/Image&gt;
    &lt;/View&gt;
&lt;/TouchableHighlight&gt;
);
function imageIconNameFor(predictionType) {
    return "Foo.png";
}
</code></pre>

<p>Why is there a different behavior when I concatenate strings to build image source?<br>
Why <code>require('../../images/'+imageIconNameFor(prediction.type)</code> fails to render image but <code>require('../../images/Foo.png')</code> works fine? <br><br>Note that method call was not a problem. Error message contains the complete path of the image computed using the method call. In spite of the complete path, react native complained about missing module with require.<br>
React native version is <strong>0.37.0</strong>. I tested this on ios-simulator.</p>