Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native FlatList horizontal mode not working at all

I am using React Native 0.44.0 and I am attempting to make a horizontal FlatList using a card style layout. For whatever reason, no matter what I do, I cannot get the horizontal mode to activate. It always seems to render vertically...

Here is the code I am using:

 <FlatList
    horizontal={true}
    data={this.state.newsFeed}
    refreshing={this.state.refreshing}
    ref={ref => {
        this.newsFeedListRef = ref;
    }}
    renderItem={this.renderNewsFeedRow.bind(this)}
    keyExtractor={(item, index) => `feed_${index}`}
    onRefresh={this.__handleNewsFeedOnRefresh.bind(this)}
    renderScrollComponent={this.renderScrollComponent.bind(this)}
    ListHeaderComponent={this.renderListHeaderComponent.bind(this)}
    getItemLayout={(data, index) => ({
        index,
        length: ITEM_HEIGHT,
        offset: ITEM_HEIGHT * index + headerBarHeight
    })} />;

I can post the code for my component renders but none of them use anything but padding and margins for style, so flex or flexDirection stuff.

like image 605
GoreDefex Avatar asked Aug 19 '17 19:08

GoreDefex


People also ask

How do you make a FlatList horizontal in React Native?

To add a horizontal FlatList in React Native, we just set the horizontal prop of the FlatList to true . to add horizontal to FlatList . As a result, we can scroll through the FlatList items horizontally.

What is keyExtractor in React Native?

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.

How do you refresh a FlatList in React Native?

To implement pull to refresh FlatList with React Native, we can set the refreshing and onRefresh props. to set the refreshing prop to isFetching , which is true when we're getting data for the FlatList . And we set onRefresh to the onRefresh which sets refreshing to true and then set it back to false after 2 seconds.


2 Answers

Try use horizontal inside flatlist tag, not value, it work for me :D

<FlatList
    horizontal
    data={tab_ad}
    renderItem={(item) => this.renderItem(item.item)}
    keyExtractor={(item, index) => index}
    ={this.state}
/>
like image 154
Nguyễn Phúc Avatar answered Oct 17 '22 19:10

Nguyễn Phúc


For anyone coming in off of a Google search, I figured it out. Turns out you can't render your own scroll component if you want to be able to dynamically change from horizontal to vertical or vice versa. So, if I take my previous code and comment out the call to renderScrollComponent, then it works... like so:

<FlatList
    data={this.state.newsFeed}
    refreshing={this.state.refreshing}
    horizontal={this.state.isHorizontal}
    ref={ref => { this.newsFeedListRef = ref; }}
    renderItem={this.renderNewsFeedRow.bind(this)}
    keyExtractor={(item, index) => `feed_${index}`}
    onRefresh={this.__handleNewsFeedOnRefresh.bind(this)}
    //renderScrollComponent={this.renderScrollComponent.bind(this)}
    ListHeaderComponent={this.renderListHeaderComponent.bind(this)}
    getItemLayout={(data, index) => ({ index, length: ITEM_HEIGHT, offset: (ITEM_HEIGHT * index) })} />

Also I can dynamically update the positioning like so. I add a function level const for calculating my item size like this:

const ITEM_SIZE = this.state.isHorizontal ? ITEM_HEIGHT : this.props.width;

Then I updated my getItemLayout function to look like this:

getItemLayout={(data, index) => ({ index, length: ITEM_SIZE, offset: ITEM_SIZE * index })} />

like image 5
GoreDefex Avatar answered Oct 17 '22 18:10

GoreDefex