Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-native: how to wrap FlatList items

I have a list of Terms that are returned by a query. Each is a single word. Currently my FlatList renders each word into a button on the same row (horizontal={true}) I would like the buttons to wrap like normal text would. But I absolutely do not want to use the column feature, because that would not look as natural. Is this possibly a bad use-case for FlatList? Are there any other components I could use?

    const styles = StyleSheet.create({
      flatlist: {
        flexWrap: 'wrap'
      },
      content: {
        alignItems: 'flex-start'
      }})

    render() {
        return (
          <Content>
            <Header searchBar rounded iosStatusbar="light-content" androidStatusBarColor='#000'>
              <Item>
                <Icon name="ios-search" />
                <Input onChangeText={(text) => this.setState({searchTerm: text})} value={this.state.searchTerm} placeholder="enter word"/>
                <Icon name="ios-people" />

                <Button transparent onPress={this._executeSearch}>
                <Text>Search</Text>
              </Button>
              </Item>
            </Header>

            <FlatList style={styles.flatlist} contentContainerStyle={styles.content}
                horizontal={true} data={this.state.dataSource} renderItem={({item}) =>
                      <Button>
                        <Text>{item.key}</Text>
                      </Button>
                  }>
            </FlatList>
          </Content>
        );
      }

One error message amongst others I've gotten is:

Warning: flexWrap: wrap`` is not supported with the VirtualizedList components.Consider using numColumns with FlatList instead.

like image 706
mrmagoo Avatar asked Aug 05 '17 21:08

mrmagoo


3 Answers

How I was able to wrap the components was like below

flatlist: {    flexDirection: 'column', }, 

and in my FlatList component added this props

numColumns={3} 
like image 130
Mozak Avatar answered Sep 20 '22 11:09

Mozak


You can remove the horizontal prop to achieve the wrapping effect

................
..................

<FlatList
    contentContainerStyle={{flexDirection : "row", flexWrap : "wrap"}} 
    data={this.state.dataSource} renderItem={({item}) =>
        <Button>
            <Text>{item.key}</Text>
        </Button>
    }
/>
.................
..............
like image 24
John Prem Kumar Srinivasan Avatar answered Sep 19 '22 11:09

John Prem Kumar Srinivasan


Unfortunately flexWrap is indeed unsupported in FlatLists. We are recommended to use a ScrollView instead of a FlatList to create this effect. This is the issue: https://github.com/facebook/react-native/issues/13939

like image 21
sinewave440hz Avatar answered Sep 19 '22 11:09

sinewave440hz