React Native ListView is a view component which contains the list of items and displays in a vertical scrollable list.
But this Listview component is deprecated. We have other Listview components like
In this chapter we will learn about FlatList component in React Native.
The
FlatList
component displays a scrolling list of changing, but similarly structured, data.FlatList
works well for long lists of data, where the number of items might change over time. Unlike the more genericScrollView
, theFlatList
only renders elements that are currently showing on the screen, not all the elements at once.The
FlatList
component requires two props:data
andrenderItem
.data
is the source of information for the list.renderItem
takes one item from the source and returns a formatted component to render.
Lets create a Simple FlatList Example
import React, {useState} from 'react'; import {StyleSheet, View, Text, FlatList, ListView} from 'react-native'; import {Component} from 'react';
export default class Views extends Component { state = { names: [ {name: 'Name1', key: '1'}, {name: 'Name2', key: '2'}, {name: 'Name3', key: '3'}, {name: 'Name4', key: '4'}, {name: 'Name5', key: '5'}, {name: 'Name6', key: '6'}, ], };
render() { return ( <View>
<View style={mystyle.header}> <Text style={mystyle.boldText}>FlatList Example</Text> </View> <FlatList style={mystyle.list} data={this.state.names} renderItem={({item}) => ( <View style={mystyle.listitems}> <Text style={mystyle.boldText}>{item.name}</Text> </View> )} /> </View> ); } } const mystyle = StyleSheet.create({ list: { backgroundColor: '#CCD1D1', height: '100%', }, listitems: { backgroundColor: '#2ECC71', borderRadius: 8, margin: 5, padding: 10, minHeight: 40, }, boldText: { fontWeight: 'bold', color: 'white', fontSize: 20, }, header: { backgroundColor: '#117864', width: '100%', alignContent: 'center', alignItems: 'center', padding: 20, }, }); |
Output
Ruby program to add two integer numbers
how to create an array with Array.[](*args) in Ruby ?
What are the various Ruby runtimes, and how are they different?
Ruby program to check whether the given number is prime or not
Ruby program to reverse a string
Ruby program to check whether the given number is palindrome
Ruby program to print Fibonacci series
How to Replace array elements in Ruby?
Ruby program to print an array
Ruby program to check whether the given number is Armstrong
Program to Print Triangle of Numbers in Ruby
How to add/remove elements to Array in Ruby?
How to shuffle an array in Ruby?
Creating Array with Array.new(size, obj) in Ruby
Ruby program to generate random numbers
Ruby program to Calculate the factorial of given number
What are #method_missing and #send? Why are they useful?
How to Sort Array in Ruby?
How to get index of array element in Ruby
How to Get Input with Gets in Ruby
How to create two dimensional array in ruby?
Reat Native Google Maps integration