简体   繁体   English

ReactNative - FlatList 在滚动之前不会更新

[英]ReactNative - FlatList not updated until scroll

I have a problem with FlatList component which does not update until scrolled.我对FlatList组件有疑问,该组件在滚动之前不会更新。

在此处输入图像描述

I tried add log to renderItem and keyExtractor both methods called with correct data but list didn't update.我尝试将日志添加到renderItemkeyExtractor这两个方法都使用正确的数据调用,但列表没有更新。

Here is a render method:这是一个渲染方法:

render() {

    const messages = this.props.messages
    const message = this.props.message

    return (
        <View style={[styles.container]}>
            <FlatList
                ref={"flatList"}
                contentContainerStyle={styles.list}
                data={messages}
                renderItem={(listItem) => {
                    return <MessageBuble message={listItem.item}/>
                }}
                keyExtractor={(item: Message) => {
                    return item.id
                }}
            />

            <View style={[styles.textInputContainer]}>
                <TextInput
                    style={styles.textInput}
                    value={message}
                    multiline={true}
                    onChangeText={this.props.messageChanged}
                />
                <Button title={"Odeslat"} onPress={() => {
                    if (this.props.sendMessage) {
                        this.props.sendMessage(this.props.message)
                    }
                }}/>
            </View>
        </View>
    )
}

Add extraData in FlatList and retry在 FlatList 中添加 extraData 并重试

<FlatList
    extraData={this.props}
    ....

Tried the extraData, but that does not work.尝试了extraData,但这不起作用。

There was an issue on Android where content was not visible when I returned back from another page to home screen (where the flatlist was present).在 Android 上存在一个问题,当我从另一个页面返回主屏幕(存在平面列表)时,内容不可见。 The content was visible when I scrolled it a bit.当我稍微滚动它时,内容是可见的。

I assigned the main list to the extraData attribute, and could see that it changed in size via console logs.我将主列表分配给 extraData 属性,并且可以通过控制台日志看到它的大小发生了变化。 But the content remained invisible.但内容仍然是不可见的。 Finally, used最后,用

onContentSizeChange={() => {
      if (list.length > 0) {
        ref.current.scrollToOffset({ animated: true, x: 0 });
      }
    }}

and it worked.它奏效了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM