Skip to content

Commit 3fc67a4

Browse files
committed
Update ListView Basics to use FlatList, SectionList.
Summary: The new list views, FlatList and SectionList, are recommended over ListView. Built website on localhost and verified the guide is rendered correctly. ![screencapture-localhost-8079-react-native-docs-using-a-listview-html-1495834607096](https://cloud.githubusercontent.com/assets/165856/26513523/c5d2913a-4220-11e7-8c8d-68bb12c75736.png) Closes #14210 Differential Revision: D5149151 Pulled By: hramos fbshipit-source-id: f28f02ee8893c4723c73d610b96ccda51cc31410
1 parent 446ff94 commit 3fc67a4

4 files changed

Lines changed: 107 additions & 37 deletions

File tree

docs/HandlingTouches.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ A common pattern to many mobile apps is the scrollable list of items. Users inte
5656

5757
ScrollViews can scroll vertically or horizontally, and can be configured to allow paging through views using swiping gestures by using the `pagingEnabled` props. Swiping horizontally between views can also be implemented on Android using the [ViewPagerAndroid](docs/viewpagerandroid.html) component.
5858

59-
A [ListView](docs/using-a-listview.html) is a special kind of ScrollView that is best suited for displaying long vertical lists of items. It can also display section headers and footers, similar to `UITableView`s on iOS.
59+
A [FlatList](docs/using-a-listview.html) is a special kind of ScrollView that is best suited for displaying long vertical lists of items. If you want to display section headers and footers, similar to `UITableView`s on iOS, then [SectionList](docs/using-a-listview.html) is the way to go.
6060

6161
### Pinch-to-zoom
6262

docs/UsingAListView.md

Lines changed: 83 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,109 @@
11
---
22
id: using-a-listview
3-
title: Using a ListView
3+
title: Using List Views
44
layout: docs
55
category: The Basics
66
permalink: docs/using-a-listview.html
77
next: network
88
previous: using-a-scrollview
99
---
1010

11-
The `ListView` component displays a scrolling list of changing, but similarly structured, data.
11+
React Native provides a suite of components for presenting lists of data. Generally, you'll want to use either [FlatList](docs/flatlist.html) or [SectionList](docs/sectionlist.html).
1212

13-
`ListView` works well for long lists of data, where the number of items might change over time. Unlike the more generic [`ScrollView`](docs/using-a-scrollview.html), the `ListView` only renders elements that are currently showing on the screen, not all the elements at once.
13+
The `FlatList` component displays a scrolling list of changing, but similarly structured, data. `FlatList` works well for long lists of data, where the number of items might change over time. Unlike the more generic [`ScrollView`](docs/using-a-scrollview.html), the `FlatList` only renders elements that are currently showing on the screen, not all the elements at once.
1414

15-
The `ListView` component requires two props: `dataSource` and `renderRow`. `dataSource` is the source of information for the list. `renderRow` takes one item from the source and returns a formatted component to render.
15+
The `FlatList` component requires two props: `data` and `renderItem`. `data` is the source of information for the list. `renderItem` takes one item from the source and returns a formatted component to render.
1616

17-
This example creates a simple `ListView` of hardcoded data. It first initializes the `dataSource` that will be used to populate the `ListView`. Each item in the `dataSource` is then rendered as a `Text` component. Finally it renders the `ListView` and all `Text` components.
17+
This example creates a simple `FlatList` of hardcoded data. Each item in the `data` props is rendered as a `Text` component. The `FlatListBasics` component then renders the `FlatList` and all `Text` components.
1818

19-
> A `rowHasChanged` function is required to use `ListView`. Here we just say a row has changed if the row we are on is not the same as the previous row.
20-
21-
```ReactNativeWebPlayer
19+
```SnackPlayer?name=FlatList%20Basics
2220
import React, { Component } from 'react';
23-
import { AppRegistry, ListView, Text, View } from 'react-native';
24-
25-
export default class ListViewBasics extends Component {
26-
// Initialize the hardcoded data
27-
constructor(props) {
28-
super(props);
29-
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
30-
this.state = {
31-
dataSource: ds.cloneWithRows([
32-
'John', 'Joel', 'James', 'Jimmy', 'Jackson', 'Jillian', 'Julie', 'Devin'
33-
])
34-
};
21+
import { AppRegistry, FlatList, StyleSheet, Text, View } from 'react-native';
22+
23+
export default class FlatListBasics extends Component {
24+
render() {
25+
return (
26+
<View style={styles.container}>
27+
<FlatList
28+
data={[
29+
{key: 'Devin'},
30+
{key: 'Jackson'},
31+
{key: 'James'},
32+
{key: 'Joel'},
33+
{key: 'John'},
34+
{key: 'Jillian'},
35+
{key: 'Jimmy'},
36+
{key: 'Julie'},
37+
]}
38+
renderItem={({item}) => <Text style={styles.item}>{item.key}</Text>}
39+
/>
40+
</View>
41+
);
3542
}
43+
}
44+
45+
const styles = StyleSheet.create({
46+
container: {
47+
flex: 1,
48+
paddingTop: 22
49+
},
50+
item: {
51+
padding: 10,
52+
fontSize: 18,
53+
height: 44,
54+
},
55+
})
56+
57+
// skip this line if using Create React Native App
58+
AppRegistry.registerComponent('AwesomeProject', () => FlatListBasics);
59+
```
60+
61+
If you want to render a set of data broken into logical sections, maybe with section headers, then a `SectionList` is the way to go.
62+
63+
```SnackPlayer?name=SectionList%20Basics
64+
import React, { Component } from 'react';
65+
import { AppRegistry, SectionList, StyleSheet, Text, View } from 'react-native';
66+
67+
export default class SectionListBasics extends Component {
3668
render() {
3769
return (
38-
<View style={{flex: 1, paddingTop: 22}}>
39-
<ListView
40-
dataSource={this.state.dataSource}
41-
renderRow={(rowData) => <Text>{rowData}</Text>}
70+
<View style={styles.container}>
71+
<SectionList
72+
sections={[
73+
{title: 'D', data: ['Devin']},
74+
{title: 'J', data: ['Jackson', 'James', 'Jillian', 'Jimmy', 'Joel', 'John', 'Julie']},
75+
]}
76+
renderItem={({item}) => <Text style={styles.item}>{item}</Text>}
77+
renderSectionHeader={({section}) => <Text style={styles.sectionHeader}>{section.title}</Text>}
4278
/>
4379
</View>
4480
);
4581
}
4682
}
4783
84+
const styles = StyleSheet.create({
85+
container: {
86+
flex: 1,
87+
paddingTop: 22
88+
},
89+
sectionHeader: {
90+
paddingTop: 2,
91+
paddingLeft: 10,
92+
paddingRight: 10,
93+
paddingBottom: 2,
94+
fontSize: 14,
95+
fontWeight: 'bold',
96+
backgroundColor: 'rgba(247,247,247,1.0)',
97+
},
98+
item: {
99+
padding: 10,
100+
fontSize: 18,
101+
height: 44,
102+
},
103+
})
104+
48105
// skip this line if using Create React Native App
49-
AppRegistry.registerComponent('ListViewBasics', () => ListViewBasics);
106+
AppRegistry.registerComponent('AwesomeProject', () => SectionListBasics);
50107
```
51108

52-
One of the most common uses for a `ListView` is displaying data that you fetch from a server. To do that, you will need to [learn about networking in React Native](docs/network.html).
109+
One of the most common uses for a list view is displaying data that you fetch from a server. To do that, you will need to [learn about networking in React Native](docs/network.html).

docs/UsingAScrollView.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,4 @@ AppRegistry.registerComponent(
6262
() => IScrolledDownAndWhatHappenedNextShockedMe);
6363
```
6464

65-
`ScrollView` works best to present a small amount of things of a limited size. All the elements and views of a `ScrollView` are rendered, even if they are not currently shown on the screen. If you have a long list of more items that can fit on the screen, you should use a `ListView` instead. So let's [learn about the ListView](docs/using-a-listview.html) next.
65+
`ScrollView` works best to present a small amount of things of a limited size. All the elements and views of a `ScrollView` are rendered, even if they are not currently shown on the screen. If you have a long list of more items that can fit on the screen, you should use a `FlatList` instead. So let's [learn about list views](docs/using-a-listview.html) next.

website/core/SnackPlayer.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ var React = require('React');
1515

1616
const PropTypes = require('prop-types');
1717

18-
const LatestSDKVersion = '15.0.0';
18+
const LatestSDKVersion = '16.0.0';
1919
var ReactNativeToExpoSDKVersionMap = {
20+
'0.44': '17.0.0',
21+
'0.43': '16.0.0',
2022
'0.42': '15.0.0',
2123
'0.41': '14.0.0',
2224
};
@@ -51,33 +53,44 @@ var SnackPlayer = React.createClass({
5153
render() {
5254
var code = encodeURIComponent(this.props.children);
5355
var params = this.parseParams(this.props.params);
54-
var platform = params.platform ? params.platform : 'ios';
55-
var name = params.name ? decodeURIComponent(params.name) : 'Example';
56+
var platform = params.platform
57+
? params.platform
58+
: 'ios';
59+
var name = params.name
60+
? decodeURIComponent(params.name)
61+
: 'Example';
5662
var description = params.description
5763
? decodeURIComponent(params.description)
5864
: 'Example usage';
5965

6066
var optionalProps = {};
6167
var { version } = this.context;
6268
if (version === 'next') {
63-
optionalProps['data-snack-sdk-version'] = LatestSDKVersion;
69+
optionalProps[
70+
'data-snack-sdk-version'
71+
] = LatestSDKVersion;
6472
} else {
65-
optionalProps['data-snack-sdk-version'] = ReactNativeToExpoSDKVersionMap[
66-
version
67-
] || LatestSDKVersion;
73+
optionalProps[
74+
'data-snack-sdk-version'
75+
] = ReactNativeToExpoSDKVersionMap[version] ||
76+
LatestSDKVersion;
6877
}
6978

7079
return (
7180
<div className="snack-player">
72-
<div className="mobile-friendly-snack" style={{ display: 'none' }}>
81+
<div
82+
className="mobile-friendly-snack"
83+
style={{ display: 'none' }}
84+
>
7385
<Prism>
7486
{this.props.children}
7587
</Prism>
7688
</div>
7789

7890
<div
7991
className="desktop-friendly-snack"
80-
style={{ marginTop: 15, marginBottom: 15 }}>
92+
style={{ marginTop: 15, marginBottom: 15 }}
93+
>
8194
<div
8295
data-snack-name={name}
8396
data-snack-description={description}

0 commit comments

Comments
 (0)