javascript - React-Native Flatlist 会在状态发生变化时重新渲染图像

标签 javascript reactjs image react-native react-native-flatlist

设置!
我在类似聊天的界面中动态加载图像,其中的消息只是文本,或者是图像和文本的消息。目前,我让它从存储在 firebase 存储中的外部 url 加载图像,并将元数据 CacheControl 设置为允许图像缓存。

我正在通过标准 react native 库图像在相当标准的平面列表中显示图像。我正在做的一件奇怪的事情是根据平面列表数据中的状态有条件地添加一个组件。示例:

    ImageOrViewMessages = (props) => {   
                if(item != null && item.image != ''){               
                    return (
                        <View>
                            <Image source={{uri: item.image, cache: "force-cache"}} style={{display: 'flex', height: 300, flex: 1, padding: 5}} />
                        </View>
                    )
                } else {
                    return(
                        <View style={{display: 'none'}}></View>
                    )
                }
            }

这是我的平面列表:

    <FlatList
        data={this.props.unsent_messages.length > 0 && this.props.chat != '' ? this.props.unsent_messages.concat(this.props.messages) : this.props.messages}
        renderItem={({item}) => {
            var align_text = "flex-end"
            var background_color = colors.main_color_light;
            var text_color = "white"
            var stamp = "none"
            if(item.username.toLowerCase() != this.props.displayName.toLowerCase()){
                //do something differ
                align_text = "flex-start"
                background_color = colors.main_color_dark;
                text_color = "white"
                stamp = "flex"
            }
            ImageOrViewMessages = (props) => {   
                if(item != null && item.image != ''){               
                    return (
                        <View>
                            <Image source={{uri: item.image, cache: "force-cache"}} style={{display: 'flex', height: 300, flex: 1, padding: 5}} />
                        </View>
                    )
                } else {
                    return(
                        <View style={{display: 'none'}}></View>
                    )
                }
            }


            return(
                <View style={{padding: 5, minHeight: 70, alignItems: align_text, justifyContent: "center"}}>
                    <View style={{opacity: item.id.toString().includes("Unset") ? .3 : 1, backgroundColor: background_color, padding: 5, borderWidth: 0, borderColor: colors.border_color,borderRadius: 8 , width: (Dimensions.get('window').width / 2) - 10, alignSelf: align_text}}>
                        <Text style={{fontFamily: "MarkerFelt-Thin", color: text_color, fontSize: 16, textAlign: "left", flex: 1}}>{item.message}</Text>
                        {/* <Text style={{display: stamp, fontFamily: "MarkerFelt-Thin", color: colors.background_color, fontSize: 14, textAlign: "right", flex: 1}}>{item.username}</Text> */}
                        <ImageOrViewMessages />
                    </View>
                </View>
            )}
            }
            keyExtractor={item => item.image != '' ? item.image : item.id.toString()}   
            style={{display: 'flex', flex: 1, borderTopLeftRadius: 25, borderTopRightRadius: 25}}    
            ref={ref => this.flatList = ref}
            inverted={true}  
        />

问题!
图像渲染得非常好,但每次它们都必须从我的服务器下载,而不是存储在缓存中。我尝试过的事情是确保上传图像上的元数据设置正确。通过图像属性“Cache”强制缓存。我已确保列表中的每个项目都有一个唯一的 key 集,包括通过 key 提取器的图像。 我还可以在我的主页上缓存图像,没有任何问题(当它不再位于平面列表中时)

最佳答案

是的,每次都会从服务器重新渲染图像。您需要将图像缓存在默认应用程序或设备内存中。最好的方法是 FastImage用于延迟加载。

将您的图像负载替换为以下图像:

 <FastImage
    style={styles.image}
    source={{
      uri: YOUR_IMAGE_URL,
      priority: FastImage.priority.normal,
    }}
    resizeMode={FastImage.resizeMode.contain}
  />

不要忘记导入 FastImage。

import FastImage from 'react-native-fast-image'

关于javascript - React-Native Flatlist 会在状态发生变化时重新渲染图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51413650/

相关文章:

javascript - HTML5 : Why does a script tag need to be placed at the end of body tag instead of at the beginning of the body tag?

javascript - 可以将依赖跟踪与 knockout js 中的行为分开吗?

javascript - 一周中有多少人注册

javascript - 我应该从 HTMLProps<HTMLElement> 继承 react Prop 吗?

image - 非均匀插值

javascript - 如何验证服务器端的浏览器是否没有窗口引用?

javascript - 如何使用 redux-toolkit 在另一个切片的 reducer 中访问一个切片的状态

reactjs - React 不会在数组状态更新时重新渲染

android - 如何使用大位图。旋转并插入画廊

c# - 使用电子表格设备将图像添加到 Excel 标题?