Согласно документации : 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} />;
Что вы можете сделать, это построить отображение / объект:
const myImages = {
img1: require('/path/to/img1'),
img2: require('/path/to/img2'),
};
render() {
return (
<TouchableOpacity onPress={this.onItemPressed} style={styles.container}>
<Image
style={styles.stretch}
source={myImages[this.state.img1]}
/>
<Image
style={styles.stretch}
source={myImages[this.state.img2]}
/>
</TouchableOpacity>
)
}
Чтобы это работало , this.state.img1
и this.state.img2
должны быть ключом в объекте myImages
.