@@ -57,6 +57,16 @@ type OptionalProps<ItemT> = {
57
57
* Rendered at the top of all the items.
58
58
*/
59
59
ListHeaderComponent ?: ?ReactClass < any > ,
60
+ /**
61
+ * Optional custom style for multi-item rows generated when numColumns > 1.
62
+ */
63
+ columnWrapperStyle ?: StyleObj ,
64
+ /**
65
+ * A marker property for telling the list to re-render (since it implements `PureComponent`). If
66
+ * your `renderItem` function depends on anything outside of the `data` prop, stick it here and
67
+ * treat it immutably.
68
+ */
69
+ extraData ?: any ,
60
70
/**
61
71
* `getItemLayout` is an optional optimizations that let us skip measurement of dynamic content if
62
72
* you know the height of items a priori. `getItemLayout` is the most efficient, and is easy to
@@ -75,6 +85,12 @@ type OptionalProps<ItemT> = {
75
85
* If true, renders items next to each other horizontally instead of stacked vertically.
76
86
*/
77
87
horizontal ?: ?boolean ,
88
+ /**
89
+ * How many items to render in the initial batch. This should be enough to fill the screen but not
90
+ * much more. Note these items will never be unmounted as part of the windowed rendering in order
91
+ * to improve perceived performance of scroll-to-top actions.
92
+ */
93
+ initialNumToRender : number ,
78
94
/**
79
95
* Used to extract a unique key for a given item at the specified index. Key is used for caching
80
96
* and as the react key to track item re-ordering. The default extractor checks `item.key`, then
@@ -91,26 +107,30 @@ type OptionalProps<ItemT> = {
91
107
* content.
92
108
*/
93
109
onEndReached ?: ?( info : { distanceFromEnd : number } ) => void ,
110
+ /**
111
+ * How far from the end (in units of visible length of the list) the bottom edge of the
112
+ * list must be from the end of the content to trigger the `onEndReached` callback.
113
+ * Thus a value of 0.5 will trigger `onEndReached` when the end of the content is
114
+ * within half the visible length of the list.
115
+ */
94
116
onEndReachedThreshold ?: ?number ,
95
117
/**
96
118
* If provided, a standard RefreshControl will be added for "Pull to Refresh" functionality. Make
97
119
* sure to also set the `refreshing` prop correctly.
98
120
*/
99
121
onRefresh ?: ?( ) => void ,
100
122
/**
101
- * Called when the viewability of rows changes, as defined by the
102
- * `viewablePercentThreshold` prop.
123
+ * Called when the viewability of rows changes, as defined by the `viewabilityConfig` prop.
103
124
*/
104
- onViewableItemsChanged ?: ?( info : { viewableItems : Array < ViewToken > , changed : Array < ViewToken > } ) => void ,
125
+ onViewableItemsChanged ?: ?( info : {
126
+ viewableItems : Array < ViewToken > ,
127
+ changed : Array < ViewToken > ,
128
+ } ) => void ,
105
129
legacyImplementation ?: ?boolean ,
106
130
/**
107
131
* Set this true while waiting for new data from a refresh.
108
132
*/
109
133
refreshing ?: ?boolean ,
110
- /**
111
- * Optional custom style for multi-item rows generated when numColumns > 1
112
- */
113
- columnWrapperStyle ?: StyleObj ,
114
134
/**
115
135
* See `ViewabilityHelper` for flow type and further documentation.
116
136
*/
@@ -148,8 +168,71 @@ type DefaultProps = typeof defaultProps;
148
168
* renderItem={({item}) => <Text>{item.key}</Text>}
149
169
* />
150
170
*
171
+ * More complex example demonstrating `PureComponent` usage for perf optimization and avoiding bugs.
172
+ *
173
+ * - By binding the `onPressItem` handler, the props will remain `===` and `PureComponent` will
174
+ * prevent wasteful re-renders unless the actual `id`, `selected`, or `title` props change, even
175
+ * if the inner `SomeOtherWidget` has no such optimizations.
176
+ * - By passing `extraData={this.state}` to `FlatList` we make sure `FlatList` itself will re-render
177
+ * when the `state.selected` changes. Without setting this prop, `FlatList` would not know it
178
+ * needs to re-render any items because it is also a `PureComponent` and the prop comparison will
179
+ * not show any changes.
180
+ * - `keyExtractor` tells the list to use the `id`s for the react keys.
181
+ *
182
+ *
183
+ * class MyListItem extends React.PureComponent {
184
+ * _onPress = () => {
185
+ * this.props.onPressItem(this.props.id);
186
+ * };
187
+ *
188
+ * render() {
189
+ * return (
190
+ * <SomeOtherWidget
191
+ * {...this.props}
192
+ * onPress={this._onPress}
193
+ * />
194
+ * )
195
+ * }
196
+ * }
197
+ *
198
+ * class MyList extends React.PureComponent {
199
+ * state = {selected: (new Map(): Map<string, boolean>)};
200
+ *
201
+ * _keyExtractor = (item, index) => item.id;
202
+ *
203
+ * _onPressItem = (id: string) => {
204
+ * // updater functions are preferred for transactional updates
205
+ * this.setState((state) => {
206
+ * // copy the map rather than modifying state.
207
+ * const selected = new Map(state.selected);
208
+ * selected.set(id, !state.get(id)); // toggle
209
+ * return {selected};
210
+ * });
211
+ * };
212
+ *
213
+ * _renderItem = ({item}) => (
214
+ * <MyListItem
215
+ * id={item.id}
216
+ * onPressItem={this._onPressItem}
217
+ * selected={!!this.state.selected.get(item.id)}
218
+ * title={item.title}
219
+ * />
220
+ * );
221
+ *
222
+ * render() {
223
+ * return (
224
+ * <FlatList
225
+ * data={this.props.data}
226
+ * extraData={this.state}
227
+ * keyExtractor={this._keyExtractor}
228
+ * renderItem={this._renderItem}
229
+ * />
230
+ * );
231
+ * }
232
+ * }
233
+ *
151
234
* This is a convenience wrapper around [`<VirtualizedList>`](docs/virtualizedlist.html),
152
- * and thus inherits the following caveats:
235
+ * and thus inherits it's props that aren't explicitly listed here along with the following caveats:
153
236
*
154
237
* - Internal state is not preserved when content scrolls out of the render window. Make sure all
155
238
* your data is captured in the item data or external stores like Flux, Redux, or Relay.
@@ -305,6 +388,7 @@ class FlatList<ItemT> extends React.PureComponent<DefaultProps, Props<ItemT>, vo
305
388
arr . push ( { ...v , item, key : keyExtractor ( item , index ) , index} ) ;
306
389
} ) ;
307
390
}
391
+
308
392
_onViewableItemsChanged = ( info ) => {
309
393
const { numColumns, onViewableItemsChanged} = this . props ;
310
394
if ( ! onViewableItemsChanged ) {
0 commit comments