简体   繁体   English

渲染后如何在不刷新 React Native 上的其他项目的情况下更改 FlatList 项目的 state

[英]How to change state of FlatList item after rendered without refreshing other item on React Native

There is a problem that all item refresh when selectedIndex changed because the selectedIndex is dependency of renderItem .有一个问题,当selectedIndex更改时所有项目都刷新,因为selectedIndexrenderItem的依赖项。 I want to manage selectedIndex to globally cause just one or none item's detail have to enabled.我想管理 selectedIndex 以在全局范围内导致只启用一个项目的详细信息或不启用任何项目的详细信息。 Is there any solution to refresh just one item that selectedIndex pointed?有什么解决方案可以只刷新selectedIndex指向的一项吗?

Here is my code这是我的代码

interface ItemType {
  title: string;
  detail: string;
}

const FlatListTestScreen = () => {
  const [selectedIndex, setSelectedIndex] = useState<number | null>(null);

  const data: ItemType[] = [
    { title: 'title1', detail: 'detail1' },
    { title: 'title2', detail: 'detail2' },
    { title: 'title3', detail: 'detail3' },
    { title: 'title4', detail: 'detail4' },
    { title: 'title5', detail: 'detail5' },
  ];

  const renderItem = useCallback<ListRenderItem<ItemType>>(
    ({ item, index }) => {
      console.log('render', index, selectedIndex === index);
      return (
        <Pressable
          onPress={() => {
            console.log('press', index);
            setSelectedIndex(index === selectedIndex ? null : index);
          }}>
          <Text>{item.title}</Text>
          {selectedIndex === index && <Text>{item.detail}</Text>}
        </Pressable>
      );
    },
    [selectedIndex],
  );

  return <FlatList data={data} renderItem={renderItem} />;
};
 LOG  render 0 false
 LOG  render 1 false
 LOG  render 2 false
 LOG  render 3 false
 LOG  render 4 false
 LOG  press 1
 LOG  render 0 false <- unnecessary render
 LOG  render 1 true
 LOG  render 2 false <- unnecessary render
 LOG  render 3 false <- unnecessary render
 LOG  render 4 false <- unnecessary render

Check my answer about FlatList optimization here: react native flatlist with and without pagination在这里查看我关于 FlatList 优化的回答: react native flatlist with and without pagination

In your case, you need to provide keyExtractor在您的情况下,您需要提供keyExtractor

Also a very useful article from the official documentation: https://reactnative.dev/docs/optimizing-flatlist-configuration还有一篇来自官方文档的非常有用的文章: https://reactnative.dev/docs/optimizing-flatlist-configuration

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

相关问题 react-native 如何在“触摸”后更改在 FlatList 中呈现的项目的样式? - react-native How to change an item's style rendered in FlatList after "touch" it? React Native:当按下“like”按钮时,如何为呈现的 FlatList 中的每个项目处理 state? - React Native: how to handle state for each item in a rendered FlatList when pressing the 'like' button? React Native Flatlist ListHeaderComponent 未在 state 更改上呈现 - React Native Flatlist ListHeaderComponent is not rendered on state change 项目后反应原生动画平面列表项目 - react native animated flatlist item after item 如何在 React Native 的平面列表中更改所选项目的颜色? - How can I change the color of selected item in flatlist in React Native? 选择项目后如何保持滚动 position 反应原生平面列表 - How to keep scroll position after selecting an item react native flatlist 如何在React Native中将渲染项目设置为状态? - How do I set a rendered item as a state in React Native? 单击项目后在FlatList中对本机更改颜色做出反应(instagram之类的) - react native change color one item in FlatList after click item (instagram like) 如何更新 FlatList 中的单个项目 - React Native - How to update a single item in FlatList - React Native 如何在 AsyncStorage 中保存 FlatList 的项目反应原生? - How to save item of FlatList in AsyncStorage react native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM