Categories
JavaScript Answers React Native Answers

How to change the height of a FlatList in React Native?

Spread the love

Sometimes, we want to change the height of a FlatList in React Native.

In this article, we’ll look at how to change the height of a FlatList in React Native.

How to change the height of a FlatList in React Native?

To change the height of a FlatList in React Native, we can set the height of the View that we wrap around the FlatList.

For instance, we write:

import * as React from 'react';
import { FlatList, Text, View } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';

const flatListItems = Array(200)
  .fill()
  .map((_, i) => ({ title: i, id: i }));

const Item = ({ title }) => (
  <View
    style={{
      backgroundColor: '#f9c2ff',
      padding: 20,
      marginVertical: 8,
      marginHorizontal: 16,
    }}>
    <Text>{title}</Text>
  </View>
);

export default function App() {
  const renderItem = ({ item }) => <Item title={item.title} />;

  return (
    <View style={{ height: 300 }}>
      <FlatList
        data={flatListItems}
        renderItem={renderItem}
        keyExtractor={(item) => item.id}
      />
    </View>
  );
}

to set the height of the View to 300px to make the height of the FlatList 300px.

Conclusion

To change the height of a FlatList in React Native, we can set the height of the View that we wrap around the FlatList.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *