mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-05-18 04:13:51 +08:00
Make "The Basics" flow like a linear tutorial
Summary: Closes https://github.com/facebook/react-native/pull/8429 Differential Revision: D3487369 Pulled By: lacker fbshipit-source-id: 59b32f2a2a67370192c91dc43da3d4b76a43b810
This commit is contained in:
committed by
Facebook Github Bot 0
parent
0e07c36794
commit
bfb4c054f4
51
docs/UsingAListView.md
Normal file
51
docs/UsingAListView.md
Normal file
@@ -0,0 +1,51 @@
|
||||
---
|
||||
id: using-a-listview
|
||||
title: Using a ListView
|
||||
layout: docs
|
||||
category: The Basics
|
||||
permalink: docs/using-a-listview.html
|
||||
next: network
|
||||
---
|
||||
|
||||
The `ListView` component displays a vertically scrolling list of changing, but similarly structured, data.
|
||||
|
||||
`ListView` works well for long lists of data, where the number of items might change over time. Unlike the more generic [`ScrollView`](/react-native/docs/using-a-scrollview.html), the `ListView` only renders elements that are currently showing on the screen, not all the elements at once.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
> 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.
|
||||
|
||||
```JavaScript
|
||||
import React, { Component } from 'react';
|
||||
import { AppRegistry, ListView, Text, View } from 'react-native';
|
||||
|
||||
class ListViewBasics extends Component {
|
||||
// Initialize the hardcoded data
|
||||
constructor(props) {
|
||||
super(props);
|
||||
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
|
||||
this.state = {
|
||||
dataSource: ds.cloneWithRows([
|
||||
'John', 'Joel', 'James', 'Jimmy', 'Jackson', 'Jillian', 'Julie', 'Devin'
|
||||
])
|
||||
};
|
||||
}
|
||||
render() {
|
||||
return (
|
||||
<View style={{paddingTop: 22}}>
|
||||
<ListView
|
||||
dataSource={this.state.dataSource}
|
||||
renderRow={(rowData) => <Text>{rowData}</Text>}
|
||||
/>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// App registration and rendering
|
||||
AppRegistry.registerComponent('AwesomeProject', () => ListViewBasics);
|
||||
```
|
||||
|
||||
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](/react-native/docs/network.html).
|
||||
Reference in New Issue
Block a user