动态更改 React Native Flat List 中的列数 [英] Dynamically changing number of columns in React Native Flat List

查看:67
本文介绍了动态更改 React Native Flat List 中的列数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 FlatList,我想根据方向更改列数.但是,当我这样做时,我得到了红屏.根据红屏错误消息,我不太确定应该如何更改关键道具.任何帮助表示赞赏.

I have a FlatList where I want to change the number of columns based on orientation. However, I get the red screen when I do this. As per the red screen error message, I'm not quite sure how I should be changing the key prop. Any help is appreciated.

      // dynamically changing number of columns
      const numCols = orientation === constants.PORTRAIT ? 3 : 8

      <FlatList
        keyExtractor={(_, i) => i}
        numColumns={numCols} // assigning the number of columns
        horizontal={false}
        renderItem={({ item }) => <ListItem imageUrl={item.url} />}
      />}

推荐答案

来自 文档,看起来你应该做这样的事情

From the documentation, looks like you should do something like this

key={(this.state.horizontal ? 'h' : 'v')}

这篇关于动态更改 React Native Flat List 中的列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆