如何在React Native中显示动态图片

问题描述 投票:0回答:1

我尝试在“react native”中使用动态图像。但该方法出现了错误。怎么解决这个问题?

下面的代码片段工作正常。

const image = require('./path/to/home.jpg');   
return (     
    <View>      
        <Image source={image}>     
    </View>   
);

但是如果我把它改成动态字符串,

const image = require({img});   
return (     
    <View>      
        <Image source={image}>    
    </View>   
);

然后我收到一个错误。那么如何在React Native中显示动态图片。

react-native
1个回答
0
投票

只要涉及到metro,React Native就不支持动态导入。您需要写出完整的

require
路径。

请参阅讨论以了解更多详细信息

© www.soinside.com 2019 - 2024. All rights reserved.