简体   繁体   English

FlatList、Props ListHeaderComponent 和动态 flatlist 数据

[英]FlatList, Props ListHeaderComponent and Dynamic flatlist data

Sorry for the title, I wasn't inspired, and I didn't know what title to put for my problem.抱歉标题,我没有受到启发,而且我不知道该给我的问题取什么标题。
I'm using Socket.io on my React native application to render some dynamics datas.我在我的 React 本机应用程序上使用Socket.io来渲染一些动态数据。
I'm using FlatList to display all this datas, and I need to show some contents like filters and buttons at the top of the FlatList .我正在使用FlatList来显示所有这些数据,并且我需要在FlatList的顶部显示一些内容,例如过滤器和按钮。
So In my FlatList Props, I used ListHeaderComponent and in this component I display one other flatlist and contents.所以在我的FlatList Props 中,我使用ListHeaderComponent ,在这个组件中我显示了另一个平面列表和内容。
My problem is:我的问题是:

When the main FlatList is updated with one of the Socket.io response (Every 5sec), all the components displayed by ListHeaderComponent aren't usable for a bit of time.当使用Socket.io响应之一(每 5 秒)更新主FlatList时, ListHeaderComponent显示的所有组件在一段时间内都无法使用。
This period is like 500ms or less, but in the ListHeaderComponent I have some buttons / filters, and they are not clickable during this period.这段时间大概是 500 毫秒或更短,但是在ListHeaderComponent中我有一些按钮/过滤器,在这段时间内它们是不可点击的。
And the problem is repeated each time the FlatList data is updated.并且每次更新 FlatList 数据时都会重复该问题。
You can see my code (simplyfied) here:你可以在这里看到我的代码(简化):

export default function MyList({ navigation }) {
    const [myData, setMyData] = useState<ResponseType[]>([])

    // Socket Io
    const socket = io(SOCKET_URL, { forceNew: true, timeout: 5000 })

    useEffect(() => {
        socket.on("connect", () => {
            console.log("Connected to Socket ID : ", socket.id)
        })

        return () => {
            socket.disconnect()
            socket.close()
        }
    },[])

    socket.on("socketResponse", (data: ResponseType) => {
        setMyData(prevDate => [...prevDate, data])
    })

    const renderFilterItem = ({ item }: { item: Filter }) => <Text key={ item.id }>{ item.title }</Text>
    const renderListItem = ({ item }: { item: ResponseType }) => <Text key={ item.id }>{ item.id }</Text>

    const ListTop = () => (
        <>
            <TouchableOpacity onPress={() => navigation.navigate('HomePage')}>
                <Text>BACK CONTENT</Text>
            </TouchableOpacity>
            <FlatList
                horizontal
                showsHorizontalScrollIndicator={false}
                data={someFilters}
                keyExtractor={(item) => item.id}
                renderItem={renderFilterItem}
            />
        </>
    )

    return (
        <FlatList
            ListHeaderComponent={ListTop}
            data={myData}
            keyExtractor={(item) => item.id}
            renderItem={renderListItem}
            maxToRenderPerBatch={5}
            windowSize={2}
        />
    )
}

I don't know how to explain better my problem.我不知道如何更好地解释我的问题。 But If I move the component of ListHeaderComponent above the FlatList, there is no more problem.但是如果我把 ListHeaderComponent 的组件ListHeaderComponent上面,就没有问题了。
But If I do this, just the FlatList is scrollable, and I want all the page scrollable.但如果我这样做,只有 FlatList 是可滚动的,我希望所有页面都是可滚动的。
I can't wrap my Flatlist with a ScrollView because of this error: VirtualizedLists should never be nested inside plain ScrollViews with the same orientation由于此错误,我无法用ScrollView包装我的 Flatlist: VirtualizedLists 永远不应嵌套在具有相同方向的普通 ScrollViews 中

Has anyone faced this problem?有没有人遇到过这个问题?

Thanks谢谢

try to use optimization for ListTop component as a possible problem here would be rendering of your functional component due to change of parent one.尝试对 ListTop 组件使用优化,因为这里可能出现的问题是由于父组件的更改而呈现您的功能组件。

here is the code sample.这是代码示例。

import React, {useCallback} from 'react';

// your other codes
const ListTop = useCallback(() => (
        <>
            <TouchableOpacity onPress={() => navigation.navigate('HomePage')}>
                <Text>BACK CONTENT</Text>
            </TouchableOpacity>
            <FlatList
                horizontal
                showsHorizontalScrollIndicator={false}
                data={someFilters}
                keyExtractor={(item) => item.id}
                renderItem={renderFilterItem}
            />
        </>
    ), [someFilters]);

here the component will be memoized until there is no change in someFilters.这里组件将被记忆,直到 someFilters 没有变化。

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

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