-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathflatlist.ejs
117 lines (101 loc) · 3.96 KB
/
flatlist.ejs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import * as React from 'react'
import { View, Text, FlatList } from 'react-native'
import { connect } from 'react-redux'
// More info here: https://facebook.github.io/react-native/docs/flatlist.html
// Styles
import styles from './Styles/<%= props.name %>Style'
class <%= props.name %> extends React.PureComponent {
/* ***********************************************************
* STEP 1
* This is an array of objects with the properties you desire
* Usually this should come from Redux mapStateToProps
*************************************************************/
state = {
dataObjects: [
{title: 'First Title', description: 'First Description'},
{title: 'Second Title', description: 'Second Description'},
{title: 'Third Title', description: 'Third Description'},
{title: 'Fourth Title', description: 'Fourth Description'},
{title: 'Fifth Title', description: 'Fifth Description'},
{title: 'Sixth Title', description: 'Sixth Description'},
{title: 'Seventh Title', description: 'Seventh Description'}
]
}
/* ***********************************************************
* STEP 2
* `renderRow` function. How each cell/row should be rendered
* It's our best practice to place a single component here:
*
* e.g.
return <MyCustomCell title={item.title} description={item.description} />
*************************************************************/
renderRow ({item}) {
return (
<View style={styles.row}>
<Text style={styles.boldLabel}>{item.title}</Text>
<Text style={styles.label}>{item.description}</Text>
</View>
)
}
/* ***********************************************************
* STEP 3
* Consider the configurations we've set below. Customize them
* to your liking! Each with some friendly advice.
*************************************************************/
// Render a header?
renderHeader = () =>
<Text style={[styles.label, styles.sectionHeader]}> - Header - </Text>
// Render a footer?
renderFooter = () =>
<Text style={[styles.label, styles.sectionHeader]}> - Footer - </Text>
// Show this when data is empty
renderEmpty = () =>
<Text style={styles.label}> - Nothing to See Here - </Text>
renderSeparator = () =>
<Text style={styles.label}> - ~~~~~ - </Text>
// The default function if no Key is provided is index
// an identifiable key is important if you plan on
// item reordering. Otherwise index is fine
keyExtractor = (item, index) => `${index}`
// How many items should be kept im memory as we scroll?
oneScreensWorth = 20
// extraData is for anything that is not indicated in data
// for instance, if you kept "favorites" in `this.state.favs`
// pass that in, so changes in favorites will cause a re-render
// and your renderItem will have access to change depending on state
// e.g. `extraData`={this.state.favs}
// Optimize your list if the height of each item can be calculated
// by supplying a constant height, there is no need to measure each
// item after it renders. This can save significant time for lists
// of a size 100+
// e.g. itemLayout={(data, index) => (
// {length: ITEM_HEIGHT, offset: ITEM_HEIGHT * index, index}
// )}
render () {
return (
<View style={styles.container}>
<FlatList
contentContainerStyle={styles.listContent}
data={this.state.dataObjects}
renderItem={this.renderRow}
keyExtractor={this.keyExtractor}
initialNumToRender={this.oneScreensWorth}
ListHeaderComponent={this.renderHeader}
ListFooterComponent={this.renderFooter}
ListEmptyComponent={this.renderEmpty}
ItemSeparatorComponent={this.renderSeparator}
/>
</View>
)
}
}
const mapStateToProps = (state) => {
return {
// ...redux state to props here
}
}
const mapDispatchToProps = (dispatch) => {
return {
}
}
export default connect(mapStateToProps, mapDispatchToProps)(<%= props.name %>)