FlatList Component

It is a React Native component that helps to make a scrolling list of given data.
Properties
  1. data - source of an element which is in array type format.
  2. renderItem - It takes an individual item of the data array and renders a component structure for it.
  3. keyExtractor - unique key for list item.
To create a list in React Native using FlatList, first import FlatList component in code.
  1. import { FlatList } from 'react-native';
Code
  1. import React, { Component } from "react";
  2. import {
  3. FlatList, Text, StyleSheet
  4. } from 'react-native';
  5. const Cities = [
  6. { id: 101, name: 'Mumbai' }, { id: 102, name: 'New York' }, { id: 103, name: 'Los Angeles' }, { id: 104, name: 'Chicago' },
  7. { id: 105, name: 'Houston' }, { id: 106, name: 'Phoenix' }, { id: 107, name: 'Philadelphia' }, { id: 108, name: 'London' },
  8. { id: 109, name: 'Birmingham' }, { id: 110, name: 'Manchester' }, { id: 111, name: 'Bangkok' }, { id: 112, name: 'Paris' },
  9. ];
  10. const extractKey = ({ id }) => id.toString()
  11. class App extends Component {
  12. renderItem = ({ item }) => {
  13. return (
  14. <Text style={styles.cityListStyle}>
  15. {item.name}
  16. </Text>
  17. )
  18. }
  19. render() {
  20. return (
  21. <FlatList
  22. style={styles.container}
  23. data={Cities}
  24. renderItem={this.renderItem}
  25. keyExtractor={extractKey}
  26. />
  27. );
  28. }
  29. }
  30. const styles = StyleSheet.create({
  31. container: {
  32. flex: 1,
  33. },
  34. cityListStyle: {
  35. padding: 15,
  36. marginBottom: 5,
  37. color: "yellow",
  38. backgroundColor: '#1A237E',
  39. fontStyle: 'italic',
  40. fontWeight: 'bold',
  41. fontFamily: "French Script MT",
  42. marginRight: 20,
  43. marginLeft: 20,
  44. borderRadius: 10,
  45. borderWidth: 1,
  46. borderColor: '#d6d7da',
  47. textAlign: 'center',
  48. fontSize: 20
  49. },
  50. })
  51. export default App;
Output for Android Platform
Output for iOS Platform
Summary
FlatList component is very easy to use in React Native to create a list view. In this blog, that’s what I discussed. In my next blog, I will talk about the SectionList component.