m00n
m00n

Reputation: 2047

How to change height of <FlatList/> in react native?

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.

Upvotes: 47

Views: 68253

Answers (10)

Sandeep Amarnath
Sandeep Amarnath

Reputation: 6917

To change the height of a <FlatList />, you can wrap it inside a <View /> and set the height of that <View />. This approach gives you better control over the layout and dimensions of your list.

Here is an example:

import React from "react";
import { FlatList, View, StyleSheet, Text } from "react-native";

const App = () => {
  const listData = [
    { key: "1" },
    { key: "2" },
    { key: "3" },
    { key: "4" },
    { key: "5" },
    { key: "6" },
    { key: "7" },
    { key: "8" },
    { key: "9" },
    { key: "10" },
  ];

  return (
    <>
      <Text style={{ fontSize: 30, textAlign: "center" }}>
        Height adjustable flatlist. You can scroll the list
      </Text>
      <View style={styles.container}>
        <View style={styles.flatListContainer}>
          <FlatList
            data={listData}
            renderItem={({ item }) => <View style={styles.item} />}
            // horizontal
            style={styles.flatList}
          />
        </View>
      </View>
    </>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: "white",
  },
  flatListContainer: {
    height: 200, // Set the desired height here
    backgroundColor: "lightblue",
    width: 100,
    alignItems: "center",
  },
  flatList: {
    // Remove height from here if it was set
  },
  item: {
    width: 30,
    height: 30,
    borderRadius: 15,
    backgroundColor: "orange",
  },
});

export default App;

Try it on Code Sandbox

Upvotes: 0

Sandeep Amarnath
Sandeep Amarnath

Reputation: 6917

To change the height of a <FlatList />, you can wrap it inside a <View /> and set the height of that <View />. This approach gives you better control over the layout and dimensions of your list.

Here is an example:

import React from 'react';
import { FlatList, View, StyleSheet } from 'react-native';

const App = () => {
  const listData = [
    { key: '1' },
    { key: '2' },
    { key: '3' },
    { key: '4' },
    { key: '5' },
  ];

  return (
    <View style={styles.container}>
      <View style={styles.flatListContainer}>
        <FlatList
          data={listData}
          renderItem={({ item }) => (
            <View style={styles.item} />
          )}
          horizontal
          style={styles.flatList}
        />
      </View>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: 'white',
  },
  flatListContainer: {
    height: 100, // Set the desired height here
    backgroundColor: 'red',
  },
  flatList: {
    // Remove height from here if it was set
  },
  item: {
    width: 30,
    height: 30,
    borderRadius: 15,
    backgroundColor: 'green',
  },
});

export default App;

Upvotes: 0

Mr.Jerry.
Mr.Jerry.

Reputation: 1159

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

flatList: {
  height: 50,
  backgroundColor: 'red',
  flexGrow: 0
}

Upvotes: 106

Sukshith S
Sukshith S

Reputation: 501

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>
  )}
/>

Upvotes: 9

Sanoob Pookodan
Sanoob Pookodan

Reputation: 25

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'
  }
});

Upvotes: -1

Muhammad Numan
Muhammad Numan

Reputation: 25353

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
    }}
    />

Upvotes: 2

Kike Gamboa
Kike Gamboa

Reputation: 1104

<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,
    }
})

Upvotes: 0

Er.pawan maan
Er.pawan maan

Reputation: 61

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>

Upvotes: 0

m00n
m00n

Reputation: 2047

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

Upvotes: 85

Viktor Hardubej
Viktor Hardubej

Reputation: 410

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

Upvotes: 2

Related Questions