【发布时间】:2018-03-23 00:37:41
【问题描述】:
我正在尝试构建类似“徽标墙”的东西,但可能存在(并且经常存在)相同的图像。渲染它们时,它们将分别加载,即使已经获取了相同的图像 url。
这是我的代码:
export default class LogoWall extends React.Component
{
//...
renderLogo(logoUrl)
{
// need to wrap into a function to have unique keys...
return (key) => <Image
key={key}
source={{uri: logoUrl}}
style={styles.logo}
/>
}
render()
{
// trying to save logos within this object
const logos = {};
return (
<View style={styles.rowContainer}>
{(() => {
let logoRows = [];
for (let i = 0; i < this.props.rows; i++) {
logoRows.push(<View style={styles.logoRow} key={i}>
{this.getLogos(i % 2 === 0).map((logoUrl, key) => {
// should only render when this image doesn't exist
if (typeof logos[logoUrl] === 'undefined') {
logos[logoUrl] = this.renderLogo(logoUrl);
}
return logos[logoUrl](key)
})}
</View>)
}
return logoRows;
})()}
</View>
)
}
}
如何改进这一点,只加载一张图片,但让它们显示多次(无需重新获取)?
【问题讨论】:
标签: javascript image react-native jsx