Skip to content

Instantly share code, notes, and snippets.

@juwencheng
Created May 22, 2017 09:26
Show Gist options
  • Save juwencheng/77143b42665d5ae1241f041177890ebc to your computer and use it in GitHub Desktop.
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"
/**
* 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)
@juwencheng
Copy link
Author

Give me a warn : A section you supplied is missing the key property
don't know

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment