flatlist with dynamic numColumns
Asked Answered
J

1

12

I start working with react native 1 month ago. Now i start building a simple that fetch data from internet and show layout like image below.

What is the best way to get these results into a grid?





enter image description here

this is my simple code

 render() {
  <FlatList
      data={newsData}
      renderItem={
        ({item,index}) =>
        index === 0 ? this._renderHighlightedVideo(item.node,index) :
        index === 5 ? this._renderAdvertisment() :
        index > 10 && (index+2) % 7 === 0 ? this._renderAdvertisment() :  this._renderGridVideo(item.node, index)
      }
      keyExtractor={(item, index) => index}
      />
 }




_renderHighlightedVideo(news, index) { }
_renderAdvertisment(){ }
_renderGridVideo(item.node, index){ }
Junk answered 12/3, 2018 at 10:22 Comment(0)
A
23
  • Use columnWrapperStyle={{ flexWrap: 'wrap'}} inFlatlist to achieve this , make sure numColumns is set greater than 1.

  <FlatList
              columnWrapperStyle={{ flexWrap: 'wrap', flex: 1, marginTop: 5 }}
              data={this.state.tags}
              horizontal={false}
              renderItem={({ item, index }) => <Tags item={item} index={index}
                handleSelectedTags={this.handleSelectedTags}
                selected={this.state.selected[index]} />
              }
              numColumns={3}
              keyExtractor={(item, index) => index}
            />

Hope this will help.

Apostolic answered 12/3, 2018 at 10:45 Comment(1)
Thank you so much @Rajat Gupta i hope you doing well in your life your amazing !!Mirthamirthful

© 2022 - 2024 — McMap. All rights reserved.