Created
March 21, 2017 04:18
-
-
Save ysfzrn/e41346355d25ced2a9e83634e851c328 to your computer and use it in GitHub Desktop.
React Native ListView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//import liraries | |
import React, { Component } from "react"; | |
import { View, Text, ListView, StyleSheet } from "react-native"; | |
const rows = [ | |
{ id: 1, text: "row1" }, | |
{ id: 2, text: "row2" }, | |
{ id: 3, text: "row3" }, | |
{ id: 4, text: "row4" }, | |
{ id: 5, text: "row5" }, | |
{ id: 6, text: "row6" } | |
]; | |
class Main extends Component { | |
constructor() { | |
super(); | |
const ds = new ListView.DataSource({ | |
rowHasChanged: (r1, r2) => r1 !== r2 | |
}); | |
this.state = { | |
list: ds.cloneWithRows(rows) | |
}; | |
} | |
render() { | |
return ( | |
<View style={styles.container}> | |
<ListView | |
dataSource={this.state.list} | |
renderRow={rowData => <Item><Text>{rowData.text}</Text></Item>} | |
/> | |
</View> | |
); | |
} | |
} | |
const Item = props => { | |
return <View style={styles.item}>{props.children}</View>; | |
}; | |
// define your styles | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
justifyContent: "center", | |
alignItems: "center", | |
backgroundColor: "#2c3e50", | |
marginTop: 30 | |
}, | |
item: { | |
height: 50, | |
width: "100%", | |
backgroundColor: "white", | |
borderBottomWidth: 1, | |
borderBottomColor: "black" | |
} | |
}); | |
//make this component available to the app | |
export default Main; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment