Skip to content

Commit a345748

Browse files
sahrensfacebook-github-bot
authored andcommitted
Better ListView - FlatList
Summary: We really need a better list view - so here it is! Main changes from existing `ListView`: * Items are "virtualized" to limit memory - that is, items outside of the render window are unmounted and their memory is reclaimed. This means that instance state is not preserved when items scroll out of the render window. * No `DataSource` - just a simple `data` prop of shape `Array<any>`. By default, they are expected to be of the shape `{key: string}` but a custom `rowExtractor` function can be provided for different shapes, e.g. graphql data where you want to map `id` to `key`. Note the underlying `VirtualizedList` is much more flexible. * Fancy `scrollTo` functionality: `scrollToEnd`, `scrollToIndex`, and `scrollToItem` in addition to the normal `scrollToOffset`. * Built-in pull to refresh support - set set the `onRefresh` and `refreshing` props. * Rendering additional rows is usually done with low priority, after any interactions/animations complete, unless we're about to run out of rendered content. This should help apps feel more responsive. * Component props replace render functions, e.g. `ItemComponent: ReactClass<{item: Item, index: number}>` replaces `renderRow: (...) => React.Element<*>` * Supports dynamic items automatically by using `onLayout`, or `getItemLayout` can be provided for a perf boost and smoother `scrollToIndex` and scroll bar behavior. * Visibility callback replaced with more powerful viewability callback and works in vertical and horizontal mode on at least Android and iOS, but probably other platforms as well. Extra power comes from the `viewablePercentThreshold` that lets the client decide when an item should be considered viewable. Demo: https://www.facebook.com/groups/576288835853049/permalink/753923058089625/ Reviewed By: yungsters Differential Revision: D4412469 fbshipit-source-id: e2d891490bf76fe14df49294ecddf78a58adcf23
1 parent 57daad9 commit a345748

File tree

12 files changed

+1896
-18
lines changed

12 files changed

+1896
-18
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/**
2+
* Copyright (c) 2013-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* The examples provided by Facebook are for non-commercial testing and
10+
* evaluation purposes only.
11+
*
12+
* Facebook reserves all rights not expressly granted.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
17+
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18+
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*
21+
* @flow
22+
*/
23+
'use strict';
24+
25+
const React = require('react');
26+
const ReactNative = require('react-native');
27+
const {
28+
StyleSheet,
29+
View,
30+
} = ReactNative;
31+
32+
const FlatList = require('FlatList');
33+
const UIExplorerPage = require('./UIExplorerPage');
34+
35+
const infoLog = require('infoLog');
36+
37+
const {
38+
FooterComponent,
39+
HeaderComponent,
40+
ItemComponent,
41+
PlainInput,
42+
SeparatorComponent,
43+
genItemData,
44+
getItemLayout,
45+
pressItem,
46+
renderSmallSwitchOption,
47+
} = require('./ListExampleShared');
48+
49+
class FlatListExample extends React.PureComponent {
50+
static title = '<FlatList>';
51+
static description = 'Performant, scrollable list of data.';
52+
53+
state = {
54+
data: genItemData(1000),
55+
horizontal: false,
56+
filterText: '',
57+
fixedHeight: true,
58+
logViewable: false,
59+
virtualized: true,
60+
};
61+
_onChangeFilterText = (filterText) => {
62+
this.setState({filterText});
63+
};
64+
_onChangeScrollToIndex = (text) => {
65+
this._listRef.scrollToIndex({viewPosition: 0.5, index: Number(text)});
66+
};
67+
render() {
68+
const filterRegex = new RegExp(String(this.state.filterText), 'i');
69+
const filter = (item) => (filterRegex.test(item.text) || filterRegex.test(item.title));
70+
const filteredData = this.state.data.filter(filter);
71+
return (
72+
<UIExplorerPage
73+
noSpacer={true}
74+
noScroll={true}>
75+
<View style={styles.searchRow}>
76+
<PlainInput
77+
onChangeText={this._onChangeFilterText}
78+
placeholder="Search..."
79+
value={this.state.filterText}
80+
/>
81+
<PlainInput
82+
onChangeText={this._onChangeScrollToIndex}
83+
placeholder="scrollToIndex..."
84+
style={styles.searchTextInput}
85+
/>
86+
<View style={styles.options}>
87+
{renderSmallSwitchOption(this, 'virtualized')}
88+
{renderSmallSwitchOption(this, 'horizontal')}
89+
{renderSmallSwitchOption(this, 'fixedHeight')}
90+
{renderSmallSwitchOption(this, 'logViewable')}
91+
</View>
92+
</View>
93+
<FlatList
94+
HeaderComponent={HeaderComponent}
95+
FooterComponent={FooterComponent}
96+
ItemComponent={this._renderItemComponent}
97+
SeparatorComponent={SeparatorComponent}
98+
disableVirtualization={!this.state.virtualized}
99+
getItemLayout={this.state.fixedHeight ? this._getItemLayout : undefined}
100+
horizontal={this.state.horizontal}
101+
data={filteredData}
102+
key={(this.state.horizontal ? 'h' : 'v') + (this.state.fixedHeight ? 'f' : 'd')}
103+
legacyImplementation={false}
104+
onRefresh={() => alert('onRefresh: nothing to refresh :P')}
105+
refreshing={false}
106+
onViewableItemsChanged={this._onViewableItemsChanged}
107+
ref={this._captureRef}
108+
shouldItemUpdate={this._shouldItemUpdate}
109+
/>
110+
</UIExplorerPage>
111+
);
112+
}
113+
_captureRef = (ref) => { this._listRef = ref; };
114+
_getItemLayout = (data: any, index: number) => {
115+
return getItemLayout(data, index, this.state.horizontal);
116+
};
117+
_renderItemComponent = ({item}) => {
118+
return (
119+
<ItemComponent
120+
item={item}
121+
horizontal={this.state.horizontal}
122+
fixedHeight={this.state.fixedHeight}
123+
onPress={this._pressItem}
124+
/>
125+
);
126+
};
127+
_shouldItemUpdate(prev, next) {
128+
/**
129+
* Note that this does not check state.horizontal or state.fixedheight because we blow away the
130+
* whole list by changing the key in those cases. Make sure that you do the same in your code,
131+
* or incorporate all relevant data into the item data, or skip this optimization entirely.
132+
*/
133+
return prev.item !== next.item;
134+
}
135+
// This is called when items change viewability by scrolling into or out of the viewable area.
136+
_onViewableItemsChanged = (info: {
137+
changed: Array<{
138+
key: string, isViewable: boolean, item: any, index: ?number, section?: any
139+
}>
140+
}
141+
) => {
142+
// Impressions can be logged here
143+
if (this.state.logViewable) {
144+
infoLog('onViewableItemsChanged: ', info.changed.map((v) => ({...v, item: '...'})));
145+
}
146+
};
147+
_pressItem = (key: number) => {
148+
pressItem(this, key);
149+
};
150+
_listRef: FlatList;
151+
}
152+
153+
154+
const styles = StyleSheet.create({
155+
options: {
156+
flexDirection: 'row',
157+
flexWrap: 'wrap',
158+
alignItems: 'center',
159+
},
160+
searchRow: {
161+
backgroundColor: '#eeeeee',
162+
padding: 10,
163+
},
164+
});
165+
166+
module.exports = FlatListExample;

0 commit comments

Comments
 (0)