Проблема в том, что .map
и FlatList
ожидают массив.Вы передаете объект.Итак, сначала вам нужно сделать так, чтобы вы передавали массив.
Вы можете сделать это, используя lodash :
Установите lodash с помощью:
npm i --save lodash
, а затем используйте его с:
var _ = require('lodash');
Теперь мы можем преобразовать ваши данные в функции рендеринга:
render() {
//get all keys, we pass them later
const keys = Object.keys(YOUR_DATA_OBJECTS);
//here we are using lodah to create an array from all the objects
const newData = _.values(YOUR_DATA_OBJECTS);
return (
<View style={styles.container}>
<FlatList
data={newData}
renderItem={({item, index}) => this.renderItem(item, keys[index])}
/>
</View>
);
}
И теперь у нас есть две вспомогательные функции рендеринга:
renderSmallItems(item){
console.log('small item', item);
// iterate over objects, create a new View and return the result
const arr = _.map(item, function(value, key) {
return (
<View key={key}>
<Text> Color: {value.color} </Text>
<Text> Time: {value.time} </Text>
</View>
);
});
return arr;
}
renderItem(item, key) {
return(
<View style={{marginBottom: 15}}>
<Text> Key: {key} </Text>
{this.renderSmallItems(item)}
</View>
);
}
Вывод:
Рабочая демонстрация:
https://snack.expo.io/HyBSpYr2E