Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flatlist with dynamic numColumns

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){ }
like image 915
Seakleng Say Avatar asked Mar 12 '18 10:03

Seakleng Say


People also ask

How can I make my FlatList faster?

Use basic components​ The more complex your components are, the slower they will render. Try to avoid a lot of logic and nesting in your list items. If you are reusing this list item component a lot in your app, create a component only for your big lists and make them with as little logic and nesting as possible.

Why FlatList React Native to keyExtractor?

keyExtractor ​ Used to extract a unique key for a given item at the specified index. Key is used for caching and as the react key to track item re-ordering. The default extractor checks item.key , then item.id , and then falls back to using the index, like React does.

What does the key extractor prop of the FlatList do?

It extracts the unique key name and its value and tells the FlatList component to track the items based on that value. The warning will then disappear after this step.


1 Answers

  • 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.

like image 89
Rajat Gupta Avatar answered Oct 27 '22 19:10

Rajat Gupta