1. 程式人生 > >RN SectionList警告missing keys for items, make sure to specify a key property on each item

RN SectionList警告missing keys for items, make sure to specify a key property on each item

警告提示每一行 item需要提供一個 key屬性,所以定義一個keyExtractor屬性指定使用id作為列表每一項的key。

export default class SectionListBasics extends Component {
    _keyExtractor=(item,index)=>item.id;
    render() {
        return (
            <View style={styles.container}>
                <SectionList
                    sections={[
                        {title: 'D'
, data: ['Devin','Jackson', 'James', 'Jillian', 'Jimmy', 'Joel', 'John', 'Julie']}, {title: 'J', data: ['Jackson', 'James', 'Jillian', 'Jimmy', 'Joel', 'John', 'Julie']}, ]} keyExtractor={this._keyExtractor} renderItem={({item}) => <Text style={styles.item}>{item}</Text>} renderSectionHeader={({section}) => <Text style={styles.sectionHeader}>{section.title}</Text>} /> </View> ); } }