29

I want to change width and height of <FlatList />.

I set the height style to the current <FlatList /> but it never worked.

I can change the height of <FlatList /> in no way.

This is my render() function and styles.

    render() {
        const listData = [];
        listData.push({ key: 0 });
        listData.push({ key: 1 });
        listData.push({ key: 2 });
        listData.push({ key: 3 });
        listData.push({ key: 4 });
        return (
            <View style={styles.container}>
                <FlatList
                    data={listData}
                    renderItem={({item}) => {
                        return (
                            <View
                                style={{
                                    width: 30, height: 30, borderRadius: 15, backgroundColor: 'green'
                                }}
                            />
                        )
                    }}
                    horizontal
                    style={styles.flatList}
                />
            </View>
        );
    }

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: 'white'
    },
    flatList: {
        height: 50,
        backgroundColor: 'red'
    }
});

And this is result of this code.

Current result

I found the answers for several hours but nothing helped me.

I am not sure why height style is not working.

Any help is appreciated.

ppianist
  • 1,659
  • 2
  • 10
  • 25

8 Answers8

74

Set the height of <View/> and place <FlatList/> inside that <View/>

Ramesh R
  • 6,273
  • 2
  • 21
  • 31
ppianist
  • 1,659
  • 2
  • 10
  • 25
37

adding flexGrow: 0 to the flatList style worked for me, so it will be:

flatList: {
  height: 50,
  backgroundColor: 'red',
  flexGrow: 0
}
Mr.Jerry.
  • 441
  • 4
  • 6
2

FlatList has prop contentContainerStyle. You can use it to style wrapper around FlatList. FlatList inherit this prop from ScrollView read hear

Viktor Hardubej
  • 319
  • 5
  • 14
1

Add flexGrow: 0. Don't mention height, the design might break when it comes to responsive

Example :

<FlatList
        style={{
          flexGrow: 0,
        }}
        data={data}
        renderItem={({item}) => (
          <View>
            <Text>{item.text}</Text>
          </View>
        )}
      />
Sukshith S
  • 129
  • 1
  • 4
0

give width then height working according to data

 <View style={{maxHeight:"50%",width:"60%"}}>
       <FlatList
            data={this.props.data}
            renderItem={({ item }) => <Text>{item.name}</Text>}
            keyExtractor={(item, index) => index}
        />
 </View>
0
<View style={styles.flatList}>
   <FlatList
      keyExtractor = { this.keyExtractor }
      data = { this.getPOs() }
      ListEmptyComponent = { this.renderEmpty }
      ItemSeparatorComponent = { Separator }
      renderItem = { this.renderItem }
   />
</View>

for me works add flex: 1 to the view

const styles = StyleSheet.create({
    flatList: {
        flex: 1,
    }
})
Kike Gamboa
  • 354
  • 2
  • 7
0

you can add flexGrow: 0 to the flatList style worked for me, so it will be:

  <FlatList
   {...{otherProps}}
    style={{
      height: 50,
      backgroundColor: 'red',
      flexGrow: 0
    }}
    />
Muhammad Numan
  • 12,108
  • 1
  • 25
  • 50
-1
render() {
    const listData = [];
    listData.push({ key: 0 });
    listData.push({ key: 1 });
    listData.push({ key: 2 });
    listData.push({ key: 3 });
    listData.push({ key: 4 });
    return (
        <View style={styles.container}>
            <FlatList
                data={listData}
                renderItem={({item}) => {
                    return (
                        <View
                            style={{
                                width: 30, height: 30, borderRadius: 15, backgroundColor: 'green'
                            }}
                        />
                    )
                }}
                horizontal
                style={styles.flatList}
            />
        </View>
    );
}

const styles = StyleSheet.create({
    container: {
        height:100
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: 'white'
    },
    flatList: {
        backgroundColor: 'red'
    }
});
David Buck
  • 3,439
  • 29
  • 24
  • 31