gpt4 book ai didi

javascript - 使用变量设置 的来源

转载 作者:行者123 更新时间:2023-11-30 20:42:11 25 4
gpt4 key购买 nike

我正在尝试一些 react native 代码,如下所示,我想将静态图像设置为 ImageView ,但它不会加载

const items = [
{ name: './images/home.png', code: '#1abc9c' }, { name: './images/home.png', code: '#2ecc71' },
{ name: './images/home.png', code: '#3498db' }, { name: './images/home.png', code: '#9b59b6' }
];

return (
<ImageBackground
source={require('./images/marble.jpg')}
style={styles.backgroundImage}>

<GridView
itemDimension={130}
items={items}
style={styles.gridView}
renderItem={item => (
<View style={[styles.itemContainer, { backgroundColor: item.code }]}>
<Image source={require(item.name)}></Image>
</View>
)}
/>

</ImageBackground>
);

我得到的错误是

calls to require expect exactly 1 string literal argument, but this was found: require(item.name).

当然,我是 React Native 的新手,所以请忽略我遗漏的术语

最佳答案

你这样试试

const items = [{ name: require('./images/home.png'), code: '#1abc9c' },{...}]

然后

<Image source={item.name}></Image>

关于javascript - 使用变量设置 <Image/> 的来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49138290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com