React Native
Lists
1
React Native Lists 1 Overview React Native provides a suite of - - PowerPoint PPT Presentation
React Native Lists 1 Overview React Native provides a suite of components for presenting lists of data. Generally use either FlatList or SectionList . The FlatList component displays a scrolling list of changing, but similarly
1
– Generally use either FlatList or SectionList.
– works well for long lists of data, where the number of items might change over time. – Unlike ScrollView, the FlatList only renders elements that are currently showing on the screen, not all the elements at once.
– Data: . the source of information for the list – renderItem. Function that takes one item from the source and returns a formatted component to render.
2
3
import React from 'react'; import { StyleSheet, Text, View, FlatList } from 'react-native’; export default class FlatListBasics extends React.Component { constructor(props){ super(props); this.state = { data: [ { key: "a" }, { key: "b" }, { key: "c" }, { key: "d" }, { key: "a longer example" }, { key: "e" }, { key: "f" }, { key: "g" }, { key: "h" }, { key: "i" }, { key: "j" }, { key: "k" }, { key: "l" }, { key: "m" }, { key: "n" }, { key: "o" }, { key: "p" } ] }; }
Each item in this list must have a variable named “key”. May also have other variables.
4
Two arguments Function that takes an item and renders it Array or list of data
5
6
constructor(props){ super(props); this.state = { data: [ { key: "a", name:"John"}, { key: "b", name: "Jesse" }, { key: "c", name: "Julie" }, { key: "d", name: "Jim" }, { key: "a longer example", name: "Joe" }, { key: "e", name: "Josephine" }, { key: "f", name: "Jerry" }, { key: "g", name: "Julianna" }, { key: "h", name: "Jessica" }, { key: "i", name: "Jennifer" }, { key: "j", name: "Jasmine" }, { key: "k", name: "Julia" }, { key: "l", name: "John" }, { key: "m", name: "Jay" }, { key: "n", name: "Jayden" }, { key: "o", name: "Jeret" }, { key: "p", name: "Judah" } ] }; }
_renderItem = data => { return <Text style={styles.row}>{data.item.key}: {data.item.name}</Text>; }; Each item in this list must have a variable named “key”. May also have other variables. Must access each variable in each object
7
8
import React from 'react'; import { StyleSheet, Text, View, FlatList, TextInput, Button, Alert } from 'react-native'; export default class FlatListBasics extends React.Component { constructor(props){ super(props); this.state = { myState: "ZZ", myState2: "No One", data: [ { key: "a", name:"John"}, { key: "b", name: "Jesse" }, { key: "c", name: "Julie" }, { key: "d", name: "Jim" }, { key: "a longer example", name: "Joe" }, { key: "e", name: "Josephine" }, { key: "f", name: "Jerry" }, { key: "g", name: "Julianna" },
{ key: "h", name: "Jessica" }, { key: "i", name: "Jennifer" }, { key: "j", name: "Jasmine" }, { key: "k", name: "Julia" }, { key: "l", name: "John" }, { key: "m", name: "Jay" }, { key: "n", name: "Jayden" }, { key: "o", name: "Jeret" }, { key: "p", name: "Judah" } ] }; } New state variables to hold text input
9
_renderItem = data => { return <Text style={styles.row}>{data.item.key}: {data.item.name}</Text>; }; updateState = () => { Alert.alert(”New key: " + this.state.myState + “ New name: “ + this.state.myState2); var newDs = []; newDs = this.state.data.slice(); newDs.push({ key:this.state.myState, name:this.state.myState2}) this.setState({ data: newDs}); };
This function is same as last example; styles a single row. Add a new value to the data array.
With no parameters it returns the entire array (which is stored in the state variable named data )
new updated array.
10
render() { return ( <View style={styles.container}> <FlatList style={{flex: 3}} data={this.state.data} renderItem={this._renderItem} /> <TextInput style={{flex:1}} style={{height: 40}} placeholder="Type here to translate!"
/> <TextInput style={{flex:1}} style={{height: 40}} placeholder="This is the second input line: type more!"
/> <Button style={{marginBottom: 100}}
title="Add Person" /> </View> ); } }
Add TextInput fields and Button When textInput values change, store in new state variables When Button pressed call event handler
11
12
_onPressButton(name) { Alert.alert('You pressed the button ' + name); } _renderItem = data => { return( <TouchableHighlight
underlayColor="yellow"> <Text style={styles.row}>{data.item.key}: {data.item.name} </Text> </TouchableHighlight> ) }; render() { return ( <View style={styles.container}> <FlatList data={this.state.data} renderItem={this._renderItem} /> </View> ); } }
Note difference in the onPress argument. Must pass a function not execute a function like before All other code remains the same.
13
14
15
This user-defined function receives two parameters: an item from the data and the index of the item. Here we just return the “id” field from the item. You must know which field in your object is unique