2016-11-11 17 views
0

どうか私の画像の場所が小道具から読み込まれていません。小道具から画像を読み込むことができません

これは

let overview_bg = require('../../images/locaties/placeholder.png'); 
<Image source={overview_bg} style={styles.overview_bg}> 

に動作します。しかし、私は小道具からURLを取得するとき、それはしていません:

Requiring unknown module "../../images/locaties/placeholder.png". If you are sure the module is there, try restarting the packager or running "npm install"

let overview_bg = require(this.props.image); 
<Image source={overview_bg} style={styles.overview_bg}> 

this.props.imageは私にエラーを与えるまったく同じURLを持っています

URLを複数回チェックしたところ、まったく同じです。私はまたそれをテストしたconsole.log

小道具は店からそれを得る別の容器から与えられる。 React Native Image documentationから

+0

Iは、第1実施例 –

+0

に拡張子を追加するのを忘れて申し訳ありませんが、私は店から取得した値が、持続Reduxの中に格納されているAJAX値です。私はこれが既に永続化されているので、これと何か関係があるかどうかはわかりません。私が必要とする正確なURLが値であることを知るエラーです。 –

+0

Davidによって指摘されているように、require()は静的リソースに対してのみ機能します。おそらく、あなたはrequire( 'something.jpg')をprops.imageとして渡す必要があります。次に、を直接設定する必要があります。 –

答えて

0

Note that in order for this to work, the image name in require has to be known statically.

// GOOD 
<Image source={require('./my-icon.png')} /> 

// BAD 
var icon = this.props.active ? 'my-icon-active' : 'my-icon-inactive'; 
<Image source={require('./' + icon + '.png')} /> 

// GOOD 
var icon = this.props.active ? require('./my-icon-active.png') : require('./my-icon-inactive.png'); 
<Image source={icon} /> 
関連する問題