concept FlatList component in category react native

appears as: FlatList component, The FlatList component
React Native in Action

This is an excerpt from Manning's book React Native in Action.

Below the Container import, create an array of data that you’ll use for the links. The items in the array will be passed to the FlatList component to create the list of links. This array should contain objects, and each object should contain a title key. You need the title key to display the name of the link:

Listing 12.4 Creating the main StarWars component
class StarWars extends Component {

  static navigationOptions = {    #1  
    #1  
  headerTitle: <Text    #1  
    style={{    #1  
      fontSize: 34, color: 'rgb(255,232,31)'    #1  
    }}    #1  
    >Star Wars</Text>,    #1  
    headerStyle: { backgroundColor: "black", height: 110 }    #1  
  }
  navigate = (link) => {    #2  
    const { navigate } = this.props.navigation
    navigate(link)
  }

  renderItem = ({ item, index }) => {    #3  
    return (
      <TouchableHighlight
        onPress={() => this.navigate(item.title)}
        style={[ styles.item, { borderTopWidth: index === 0 ? 1 : null} ]}>
        <Text style={styles.text}>{item.title}</Text>
      </TouchableHighlight>
    )
  }

  render() {    #4  
    return (    #4  
      <Container>    #4  
        <FlatList    #4  
          data={links}    #4  
          keyExtractor={(item) => item.title}    #4  
          renderItem={this.renderItem}    #4  
        />    #4  
      </Container>    #4  
    )
  }
}

const styles = StyleSheet.create({
  item: {
    padding: 20,
    justifyContent: 'center',
    borderColor: 'rgba(255,232,31, .2)',
    borderBottomWidth: 1
  },
  text: {
    color: '#ffe81f',
    fontSize: 18
  }
});

#1   Creates a static navigationOptions object, and passes in a headerTitle component and a headerStyle object
#2   Creates a navigate method that takes a link as an argument
#3   Loops through the data array and returns an item and an index for each item
#4   Returns the Container, wraps the FlatList component in it, and passes in links, renderItem, and a keyExtractor method