Created
May 22, 2017 09:26
-
-
Save juwencheng/77143b42665d5ae1241f041177890ebc to your computer and use it in GitHub Desktop.
React Native How to use SectionList, but have warning: "A section you supplied is missing the key property"
This file contains hidden or 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
/** | |
* Created by Owen Ju on 22/05/2017. | |
*/ | |
import React, {Component} from 'react' | |
import {AppRegistry, View, Text, StyleSheet, SectionList} from 'react-native' | |
// build mock data | |
const sections = [ | |
{ | |
id: 5, | |
title: "Basic Components", | |
data: [ | |
{id: 0, text: "View"}, | |
{id: 1, text: "Text"}, | |
{id: 2, text: "Image"}, | |
] | |
}, | |
{ | |
id: 6, | |
title: "List Components", | |
data: [ | |
{id: 3, text: "ScrollView"}, | |
{id: 4, text: "ListView"}, | |
] | |
}, | |
] | |
const extractKey = (item, idx) => { | |
return item.id | |
} | |
export default class MySectionList extends Component { | |
renderItem = ({item}) => { | |
return ( | |
<Text style={styles.row}> | |
{item.text} | |
</Text> | |
) | |
} | |
renderSectionHeader = ({section}) => { | |
return ( | |
<Text style={styles.header}> | |
{section.title} | |
</Text> | |
) | |
} | |
render() { | |
return ( | |
<SectionList | |
style={styles.container} | |
sections={sections} | |
renderItem={this.renderItem} | |
renderSectionHeader={this.renderSectionHeader} | |
keyExtractor={extractKey}/> | |
) | |
} | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
}, | |
row: { | |
backgroundColor: "red", | |
height: 50, | |
}, | |
header: { | |
backgroundColor: 'blue', | |
height: 50 | |
} | |
}) | |
AppRegistry.registerComponent("MySectionList", () => MySectionList) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Give me a warn : A section you supplied is missing the key property
don't know