简体   繁体   English

如何解决FlatList React native中的Key问题

[英]How can resolve problem of Key in FlatList React native

It my first experience with React, I have this problem in my program.这是我第一次使用 React,我的程序中有这个问题。 Will you help me please?请你帮帮我好吗? I use TMDB API in my application.我在我的应用程序中使用 TMDB API。

index.js:1 Warning: Encountered two children with the same key, `.$106242`. Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and could change in a future version.
        in div (created by View)
        in View (created by ScrollView)
        in div (created by View)
        in View (created by ForwardRef)
        in ForwardRef (created by ScrollView)
        in ScrollView (created by VirtualizedList)
        in VirtualizedList (created by FlatList)
        in FlatList (at Search.js:71)
        in div (created by View)
        in View (at Search.js:65)
        in Search (at App.js:8)
        in App (created by ExpoRootComponent)
        in ExpoRootComponent (created by RootComponent)
        in RootComponent
        in div (created by View)
        in View (created by AppContainer)
        in div (created by View)
        in View (created by AppContainer)
        in AppContainer
<FlatList
  data={this.state.films}
  keyExtractor={(item, index) => item.id.toString()}
  renderItem={({ item }) => <Text> <FilmItem film={item} /> </Text>}
  onEndReachedThreshold={0.5}
  onEndReachedThreshold={0.5}
  onEndReached={() => {
    if (this.page < this.totalPages) { // On vérifie qu'on n'a pas atteint la fin de la pagination (totalPages) avant de charger plus d'éléments
      this._loadFilms()
    }
  }}
/>

Any proposition任何提议

Change you flatlist code to this,将您的平面列表代码更改为此,

<FlatList
    data = {this.state.films}
    keyExtractor = {(item, index) => index+"_"+item.id.toString() }
    renderItem={({item}) => <Text> <FilmItem film={item} /> </Text>}
    onEndReachedThreshold={0.5}
    onEndReachedThreshold={0.5}
    onEndReached={() => {
        if (this.page < this.totalPages) { // On vérifie qu'on n'a pas atteint la fin de la pagination (totalPages) avant de charger plus d'éléments
            this._loadFilms()
        }
    }}
/>

you can use index or combination of index and any property of films for keyExtractor.您可以为 keyExtractor 使用索引或索引与电影的任何属性的组合。 In below example I have used index and added comment on the same line which can also be used in place of index.在下面的示例中,我使用了索引并在同一行添加了注释,它也可以用来代替索引。

<FlatList
    data = {this.state.films}
    keyExtractor = {(item, index) => index } // `${item.id.toString()}-${index}`
    renderItem={({item}) => <Text> <FilmItem film={item} /> </Text>}
    onEndReachedThreshold={0.5}
    onEndReachedThreshold={0.5}
    onEndReached={() => {
        if (this.page < this.totalPages) { // On vérifie qu'on n'a pas atteint la fin de la pagination (totalPages) avant de charger plus d'éléments
            this._loadFilms()
        }
    }}
/>

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

相关问题 反应本机 FlatList 键 - React Native FlatList key 我们如何根据索引或通过 react-native flatlist 中的键检查/取消选中 flatlist 中的单个复选框 - How can we Check/Uncheck individual checkbox in flatlist based on index or by key in react-native flatlist 如何在本机反应中呈现 FlatList 中的数据,我可以控制台记录它但在屏幕上显示它时出现问题 - How to render data in FlatList in react native, I can Console log it but have a problem to display it on Screen React Native Flatlist 动画视图滚动问题 - React Native Flatlist Animated View Scrolling Problem 基于关键 React Native 的 Flatlist 标题 - Flatlist Header on the base of key React native 如何在 React Native 的平面列表中更改所选项目的颜色? - How can I change the color of selected item in flatlist in React Native? react native:如何使用平面列表显示数据内容? - react native: How can I display the data content using a flatlist? 如何在 React Native 中使用可滚动的 FlatList 制作屏幕? - How can you make a screen with a FlatList scrollable in React Native? 我如何在 FlatList React native 中使用 scrollToIndex? - how can i use scrollToIndex in FlatList React native? React Native:我如何获取 Picker 的值并将其添加到 Flatlist - React Native: how can i Get value of Picker and add it to Flatlist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM