react / react/react-native

SectionList unnecessarily unmounts and mounts items when filtering (in between renders)

未关闭
#28,919 25 条评论 15 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Component: SectionList Needs: Attention Platform: Android
主要语言
C++
星标
127k
派生
25.3k
平均合并
1 天 23 小时
30 天内合并 PR
4

描述

Description

When a SectionList rerenders, sometimes it unmounts and remounts some of its items unnecessarily, even though the items preserve the same "key" prop in between renders. This impacts performance.

A scenario where this could happen is when filtering data. Imagine a SectionList with a search bar (TextInput) above it. Every time the user types something in the search bar, the section list gets filtered so it shows the items that contain the text in the search bar. Sometimes, the items get remounted unnecessarily when typing (Why remount an item when it was already there? It passed the filter, it should stay there.)

React Native version:

System:
OS: Windows 10 10.0.18363
CPU: (8) x64 Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
Memory: 6.52 GB / 15.87 GB
Binaries:
Node: 12.14.0 - C:\Program Files\nodejs\node.EXE
Yarn: Not Found
npm: 6.13.4 - C:\Program Files\nodejs\npm.CMD
Watchman: Not Found
SDKs:
Android SDK: Not Found
IDEs:
Android Studio: Version 3.6.0.0 AI-192.7142.36.36.6392135
Languages:
Java: Not Found
Python: 2.7.17 - C:\Python27\python.EXE
npmPackages:
@react-native-community/cli: Not Found
react: 16.11.0 => 16.11.0
react-native: 0.62.2 => 0.62.2
npmGlobalPackages:
react-native: Not Found

Steps To Reproduce

  1. Create a new react-native project. npx react-native init sectionlisttest
  2. Modify the App.js file so it looks like the following.
import React, {useEffect, useState} from 'react';
import {SectionList, Text, View} from 'react-native';

const originalData = [{
  key: 'header1',
  header: 'Header 1',
  data: [{key: 'firstItem'}, {key: 'byebye1'}],
}];

const filteredData = [{
  key: 'header1',
  header: 'Header 1',
  data: [{key: 'firstItem'}],
}];

export default function App() {
  useEffect(() => {
    setTimeout(() => setSections(filteredData), 5000);
  }, []);

  const [sections, setSections] = useState(originalData);

  return (
    <SectionList
      sections={sections}
      renderItem={renderItem}
      renderSectionHeader={renderSectionHeader}
      ItemSeparatorComponent={Separator}
    />
  );
}

const renderItem = ({item}) => <Item item={item} />;

const Item = ({item}) => {
  useEffect(() => {
    console.log('item mounted');
  }, []);
  return <Text>{item.key}</Text>;
};

const renderSectionHeader = ({section}) => <Text>{section.header}</Text>;

const Separator = () => <View style={{height: 1, backgroundColor: 'black'}} />;
  1. Run the app. I ran it in Android (npx react-native run-android).

IMPORTANT: Fully reload the app, don't depend on hot reloading.

Expected Results

Expected result: 2 lines saying "Item mounted" get printed to the logs.

Actual Result: 2 lines saying "Item mounted" get printed to the logs. . But then, 5 seconds later, it prints a third line also saying "Item mounted".

Snack, code example, screenshot, or link to a repository:

Snack: https://snack.expo.io/@hectorricardo/sectionlist-bug

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

使用五秒后的 SectionList 数据转换,复现 App.js 示例或链接的 Snack 中的行为。跟踪更新期间有哪些项目会被挂载,并检查 SectionList 的渲染行为和 key 处理。完成标准是:筛选或更新 section 时,不再不必要地挂载保留相同 key 的项目,并有一个覆盖该场景的回归测试。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, react-native
领域
mobile
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
48/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。