请教一个 react native FlatList 的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
letterLim
V2EX    React

请教一个 react native FlatList 的问题

  •  
  •   letterLim 235 天前 1815 次点击
    这是一个创建于 235 天前的主题,其中的信息可能已经有所发展或是发生改变。
    正在做一个聊天应用。 因为要有双向滚动加载的需求放弃了 FlashList 转而使用 FlatList 。 发现发消息新增 item 时。每次渲染都需要 0.8 秒左右。在这期间所有的 setState 都会卡住。 也试了文档中的优化方案感觉效果不大。 有没有大哥知道怎么解决
    4 条回复    2025-02-19 10:07:44 +08:00
    donotquestion
        1
    donotquestion  
       234 天前
    放弃吧,我研究了两周放弃了,傻逼 RN 不会按照你的想法的顺序走的,跳来跳去,双向滚动超级大坑
    iOCZS
        2
    iOCZS  
       234 天前
    相关代码看看?
    jingrui
        3
    jingrui  
       234 天前
    我们还是用的 FlashList ,滚动到指定消息 很难优化


    const firstScrollRef = useRef(false);

    function scrollToMsg(msgID){
    let index = viewMessages.findIndex((value)=>value?.msgID === msgID)
    if (index <= 0) index = 1;
    console.log('scrollToMsg', index, msgID, viewMessages.length);
    flatListRef.current.scrollToIndex({ index:index-1, animated: true });
    }

    const scrollToBottom = useCallback(
    debounce(() => {
    if (flatListRef.current) {
    console.log('scrollToBottom', firstScrollRef.current, from, msgID)
    IMSdk.readConversation(uid)
    if (!firstScrollRef.current) {
    if (from === 'history' && msgID) {
    scrollToMsg(msgID)
    } else {
    flatListRef.current.scrollToEnd({ animated: true });
    }
    firstScrollRef.current = true
    } else {
    flatListRef.current.scrollToEnd({ animated: true });
    }
    }
    }, 200), // 延迟时间可以根据需要调整
    [from, msgID]
    );
    letterLim
        4
    letterLim  
    OP
       234 天前
    const getMessageList = useMemo(() => (startPage: number, endPage: number) => {
    return allMessageList
    .slice(startPage * PAGE_SIZE, (endPage + 1) * PAGE_SIZE)
    }, [allMessageList, PAGE_SIZE])

    useEffect(() => {
    setMessageList(getMessageList(0, 0))
    }, [])

    useEffect(() => {
    if (startPage === 0 && isAtBottom) {
    setMessageList(getMessageList(startPage, endPage));
    }
    }, [startPage, endPage, getMessageList, isAtBottom]);


    const OnReachTop= useCallback(() => {
    console.log('onReachTop');
    if (isJumpingToUnread) {
    return;
    }
    if (startPage > 0) {
    const _startPage = startPage - 1;
    setStartPage(_startPage);
    setMessageList(getMessageList(_startPage, endPage))
    }
    }, [startPage, isJumpingToUnread, endPage, getMessageList]);


    const OnReachBottom= useCallback(() => {
    console.log('onReachBottom', isJumpingToUnread);
    if (isJumpingToUnread) {
    return;
    }
    if (messageList.length < allMessageList.length) {
    setEndPage(pre => {
    setMessageList(getMessageList(startPage, pre + 1))
    return pre + 1
    })
    }
    }, [messageList.length, setEndPage, startPage, allMessageList.length, isJumpingToUnread, getMessageList]);

    const goUnreadMessage = useCallback(() => {
    setIsJumpingToUnread(true);
    setUnreadNum(0)
    let _messageList: any = []
    const startPage = Math.floor(unreadNum / PAGE_SIZE) > 1 ? Math.floor(unreadNum / PAGE_SIZE) - 1 : 0;
    let endPage = startPage + 1;
    _messageList = getMessageList(startPage, endPage)
    const index = _messageList.findIndex(item => item._id === lastMessageId.current)
    if (index % PAGE_SIZE === 0) {
    endPage = startPage + 1
    }
    setStartPage(startPage);
    setEndPage(endPage);
    setMessageList(_essageList)
    }, [allMessageList.length, getMessageList]);

    useEffect(() => {
    if (isJumpingToUnread && lastMessageId.current) {
    const index = messageList.findIndex(item => item._id === lastMessageId.current)
    if (index !== -1) {
    const timer = setTimeout(() => {
    clearTimeout(timer)
    flatListRef.current?.scrollToIndex({
    index,
    animated: true,
    viewPosition: 0.1,
    })
    }, 200)
    }
    }
    }, [messageList.length, isJumpingToUnread]);


    useEffect(() => {
    if (!lastMessageId.current && allMessageList.length >= (unreadNum-1) && unreadNum) {
    lastMessageId.current = allMessageList[unreadNum-1]?._id;
    }
    }, []);

    useEffect(() => {
    const initMessageList = () => {
    setStartPage(0)
    setEndPage(0)
    setMessageList(getMessageList(0, 0))
    InteractionManager.runAfterInteractions(() => {
    flatListRef?.current?.scrollToOffset({ offset: 0, animated: true })
    })
    }
    EventCenter.on('init_message_list', initMessageList)
    return () => {
    EventCenter.remove('init_message_list', initMessageList)
    }
    }, [getMessageList])


    const handleScroll = useCallback((event: NativeSyntheticEvent<NativeScrollEvent>) => {
    const isBottom = event.nativeEvent.contentOffset.y <= 10;
    setIsAtBottom(isBottom);
    }, []);

    const OnScrollToIndexFailed= useCallback((info: {
    index: number;
    highestMeasuredFrameIndex: number;
    averageItemLength: number;
    }) => {
    const offset = info.index * info.averageItemLength
    flatListRef.current?.scrollToOffset({ offset: offset, animated: true })
    }, []);

    const threshold = isJumpingToUnread ? 0.1 : 1

    <FlatList
    removeClippedSubviews
    windowSize={20}
    OnStartReachedThreshold={threshold}
    OnEndReachedThreshold={threshold}
    OnEndReached={onReachBottom}
    OnStartReached={onReachTop}
    OnScroll={handleScroll}
    scrollEventThrottle={17}
    keyboardDismissMode="on-drag"
    maxToRenderPerBatch={10}
    initialNumToRender={10}
    OnTouchStart={() => {
    setIsMore(false)
    setEmojiKeyboardVisible(false)
    }}
    OnScrollBeginDrag={() => {
    setIsJumpingToUnread(false);
    }}
    maintainVisibleCOntentPosition={startPage && !isAtBottom ? {
    minIndexForVisible: 1,
    autoscrollToTopThreshold: 10
    }:undefined}
    OnScrollToIndexFailed={onScrollToIndexFailed}
    inverted
    cOntentContainerStyle={{ paddingHorizontal: 10 }}
    scrollsToTop={false}
    data={messageList}
    keyExtractor={item => item._id}
    showsVerticalScrollIndicator
    ref={flatListRef}
    renderItem={renderItem}
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1201 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 17:29 PVG 01:29 LAX 10:29 JFK 13:29
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86