memo化renderItem后仍遇列表更新缓慢错误,求解决方案
问题描述
我已经试过StackOverflow、各类网页和聊天机器人给出的所有方案,包括对renderItem函数做memo化、用PureComponent替代函数组件、使用useCallback,但不管用哪种,都会出现相同的UI冻结问题,根本没法忽略!更气人的是,另一个结构和renderItem逻辑几乎一样的列表,做了memo化就正常工作了!
尝试的代码
BleSections 函数
const BleSections = (params) => { if (!params.device) { return []; } const { sections: deviceSections } = params.device; return [ { title: "Address", data: [ { label: "MAC", value: params.device.bleDeviceAddress }, ], }, { title: "Temperature", data: [ { label: "Cloud ID", value: deviceSections?.Temperature?.cloud_id }, { label: "Local ID", value: deviceSections?.Temperature?.local_id }, ], }, { title: "Humidity", data: [ { label: "Cloud ID", value: deviceSections?.Humidity?.cloud_id }, { label: "Local ID", value: deviceSections?.Humidity?.local_id }, ], }, { title: "Panic", data: [ { label: "Cloud ID", value: deviceSections?.Panic?.cloud_id }, { label: "Local ID", value: deviceSections?.Panic?.local_id }, ], }, ]; }; export default BleSections;
生成 sections
const sections = BleSections({ device: Object.values(bleDevices).find(device => bleDevice && device.bleDeviceAddress === bleDevice.id) });
BleSectionRender 组件
import React, { memo } from "react"; import { View, Text } from "react-native"; import ScanStyle from "../Styles/ScanStyle.js"; import BleTextRender from "./BleTextRender.js"; const BleSectionRender = ({ item: section }) => { return ( <View style={ScanStyle.subSectionContainer}> <Text style={ScanStyle.subSectionTitle}> {section.title} </Text> {section.data.map((item, itemIndex) => ( <View key={itemIndex}> <BleTextRender label={item.label} value={item.value} /> </View> ))} </View> ); }; export default memo(BleSectionRender);
BleTextRender 组件
import React, { memo } from "react"; import { View, Text } from "react-native"; import ScanStyle from "../Styles/ScanStyle"; const BleTextRender = ({ label, value }) => { return ( <View style={ScanStyle.textContainer}> <Text style={ScanStyle.textLabel}> {label}: </Text> <Text style={ScanStyle.textValue}> {value ? value : "Not set"} </Text> </View> ); }; export default memo(BleTextRender);
FlatList 使用代码
<FlatList data={sections} renderItem={BleSectionRender} keyExtractor={(item, index) => index.toString()} contentContainerStyle={ScanStyle.content} />
内容的提问来源于stack exchange,提问作者Tycho Systems
相关产品推荐
相关产品推荐

