Ho creato un'app con ReactNative sia per iOS che per Android con estensioneListView . Quando si popola la listview con un'origine dati valida, viene stampato il seguente avviso nella parte inferiore dello schermo:
Avvertenza: ogni figlio in un array o in un iteratore dovrebbe avere un prop "chiave" univoco. Controlla il metodo di rendering di
ListView.
Qual è lo scopo di questo avvertimento? Dopo il messaggio si collegano a questa pagina , dove vengono discusse cose completamente diverse che non hanno nulla a che fare con React Native, ma con Reactjs basato sul web.
My ListView è costruito con queste istruzioni:
render() {
var store = this.props.store;
return (
<ListView
dataSource={this.state.dataSource}
renderHeader={this.renderHeader.bind(this)}
renderRow={this.renderDetailItem.bind(this)}
renderSeparator={this.renderSeparator.bind(this)}
style={styles.listView}
/>
);
}
Il mio DataSource è costituito da qualcosa come:
var detailItems = [];
detailItems.push( new DetailItem('plain', store.address) );
detailItems.push( new DetailItem('map', '') );
if(store.telefon) {
detailItems.push( new DetailItem('contact', store.telefon, 'Anrufen', 'fontawesome|phone') );
}
if(store.email) {
detailItems.push( new DetailItem('contact', store.email, 'Email', 'fontawesome|envelope') );
}
detailItems.push( new DetailItem('moreInfo', '') );
this.setState({
dataSource: this.state.dataSource.cloneWithRows(detailItems)
});
E le righe ListView vengono renderizzate con cose come:
return (
<TouchableHighlight underlayColor='#dddddd'>
<View style={styles.infoRow}>
<Icon
name={item.icon}
size={30}
color='gray'
style={styles.contactIcon}
/>
<View style={{ flex: 1}}>
<Text style={styles.headline}>{item.headline}</Text>
<Text style={styles.details}>{item.text}</Text>
</View>
<View style={styles.separator}/>
</View>
</TouchableHighlight>
);
Tutto funziona bene e come previsto, tranne l'avvertimento che mi sembra essere una totale assurdità.
L'aggiunta di una proprietà-chiave alla mia Classe "DetailItem" non ha risolto il problema.
Questo è ciò che verrà realmente passato a ListView come risultato di "cloneWithRows":
_dataBlob:
I/ReactNativeJS( 1293): { s1:
I/ReactNativeJS( 1293): [ { key: 2,
I/ReactNativeJS( 1293): type: 'plain',
I/ReactNativeJS( 1293): text: 'xxxxxxxxxx',
I/ReactNativeJS( 1293): headline: '',
I/ReactNativeJS( 1293): icon: '' },
I/ReactNativeJS( 1293): { key: 3, type: 'map', text: '', headline: '', icon: '' },
I/ReactNativeJS( 1293): { key: 4,
I/ReactNativeJS( 1293): type: 'contact',
I/ReactNativeJS( 1293): text: '(xxxx) yyyyyy',
I/ReactNativeJS( 1293): headline: 'Anrufen',
I/ReactNativeJS( 1293): icon: 'fontawesome|phone' },
I/ReactNativeJS( 1293): { key: 5,
I/ReactNativeJS( 1293): type: 'contact',
I/ReactNativeJS( 1293): text: 'xxxxxxxxx@hotmail.com',
I/ReactNativeJS( 1293): headline: 'Email',
I/ReactNativeJS( 1293): icon: 'fontawesome|envelope' },
I/ReactNativeJS( 1293): { key: 6, type: 'moreInfo', text: '', headline: '', icon: '' } ] },
Come una chiave vede, ogni record ha una proprietà chiave. L'avvertimento esiste ancora.
DetailItemè necessario che tu abbia le chiavi. Se hanno già chiavi univoche, è necessario mostrare gli altri metodi di rendering (renderHeader, renderDetailItem, renderSeparator). Funzionano bene e si aspettano fino a quando l'origine dati non viene modificata in qualche modo (le righe vengono rimosse, ad esempio), a quel punto React non saprà cosa farne quando non hanno un identificatore univoco.