简体   繁体   English

React Native FlatList 只渲染 10 个项目

[英]React Native FlatList only renders 10 items

I'm trying to display a FlatList with a dataset of 86 items and it's only displaying 10 and will not load more.我正在尝试显示一个包含 86 个项目的数据集的 FlatList,它只显示 10 个并且不会加载更多。

I tried messing with the container size through styles but no avail.我尝试通过 styles 弄乱容器大小,但无济于事。

return (
  <View>
    <Text>{this.state.cards.length}</Text>
    <FlatList
      data={this.state.cards}
      renderItem={(theInfo) => <CardImage key={theInfo.key} info={theInfo}/>}
      keyExtractor={(item, index) => item.toString()}
    />
  </View>
);

I expect this to display 86 items (this.state.cards.length displays 86), the application only displays 10 and will not load more.我希望它显示 86 个项目(this.state.cards.length 显示 86),应用程序只显示 10 个并且不会加载更多。

Edit: rn version 0.57.8编辑:rn 版本 0.57.8

You should set following property您应该设置以下属性

initialNumToRender={50}

As the default is 10因为默认是 10

Source: https://facebook.github.io/react-native/docs/flatlist#initialnumtorender来源: https : //facebook.github.io/react-native/docs/flatlist#initialnumtoender

Change View to ScrollView将视图更改为 ScrollView

Updated Code:更新代码:

return (
  <ScrollView>
    <Text>{this.state.cards.length}</Text>
    <FlatList
      data={this.state.cards}
      renderItem={(theInfo) => <CardImage key={theInfo.key} info={theInfo}/>}
      keyExtractor={(item, index) => item.toString()}
    />
  </ScrollView>
);

Using initialNumToRender is not a proper solution for this.使用 initialNumToRender 不是一个合适的解决方案。 It affects the processing as the whole list is rendered by flatList .它会影响处理,因为整个列表由flatList呈现。 If you're using animation anywhere in your project then you can add isInteraction: false parameter in Animated.timing() eg-如果您在项目中的任何地方使用动画,那么您可以在Animated.timing()添加isInteraction: false参数,例如-

Animated.timing(
    this.spinValue,
    {
      toValue: -1,
      duration: 4000,
      easing: Easing.linear,
      isInteraction: false
    }
  ).start( ()=> this.spin() )

for more information, you can see this comment in react-native project's issues.有关更多信息,您可以在 react-native 项目的问题中看到评论。

Hope this will help you!希望能帮到你!

I was struggling with the same problem, the only thing that worked from amongst these answers was Arjun Jain's but the moment you do that the FlatList can no longer determine the correct number of items to render and ceases to be efficient (and in my case, the list of items is 2000+ entries so this matters).我正在为同样的问题苦苦挣扎,这些答案中唯一有效的是Arjun Jain 的,但是当您这样做时, FlatList无法再确定要呈现的正确项目数并且不再有效(在我的情况下,项目列表是 2000 多个条目,所以这很重要)。

After loads of fiddling, what turned out to be missing in my code was that the rendered element (in this case, CardImage , in mine it was a ListItem ) just needs an item attribute:经过大量的摆弄之后,我的代码中缺少的是渲染元素(在本例中为CardImage ,在我的中为ListItem )只需要一个item属性:

return (
  <SafeAreaView>
    <Text>{this.state.cards.length}</Text>
    <FlatList
      data={this.state.cards}
      renderItem={(theInfo) => <CardImage item={theInfo} key={theInfo.key} info={theInfo}/>}
      keyExtractor={(item, index) => item.toString()}
    />
  </SafeAreaView>
);

The issue appeared after updating React Native from version 62 to 64. My solution was to hide the FlatList when there are not elements to show.将 React Native 从版本 62 更新到 64 后出现了该问题。我的解决方案是在没有要显示的元素时隐藏 FlatList。

return (
  <View>
   <Text>{this.state.cards.length}</Text>
   {this.state.cards.length > 0 &&
   <FlatList
     data={this.state.cards}
     renderItem={(theInfo) => <CardImage key={theInfo.key} info={theInfo}/>}
     keyExtractor={(item, index) => item.toString()}
   />
   }
  </View>
);

Also Arjun Jain's solution fixes the issue. Arjun Jain 的解决方案也解决了这个问题。

I have solved my problem in a different way, maybe it helps someone...我以不同的方式解决了我的问题,也许它可以帮助某人......

For me, setting initialNumToRender={50} didn't solv all the problems, it caused another problem.对我来说,设置initialNumToRender={50}并没有解决所有问题,它导致了另一个问题。

To fix it, in my case I had:要修复它,就我而言,我有:

    renderItem({ item }) {
    return <MyButton 
      key={String(item._id)}
      ... other props
    />

At the style of MyButton, I had as the container style:在 MyButton 的样式中,我将其作为容器样式:

flex: 1
height: 68px;
padding-right: 10px;
margin-top: 15px;
flex-direction: row;

So I changed it to:所以我将其更改为:

height: 68px;
padding-right: 10px;
margin-top: 15px;
flex-direction: row;

and its fixed!!它是固定的! flex: 1 , was causing the FlatList to render empty spaces for fields that was not rendered... Weird! flex: 1 ,导致 FlatList 为未呈现的字段呈现空白空间......很奇怪!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM