Reputation: 675
I am attempting to populate a FlatList
of custom components (<EventCard>
) on a screen, however, none of the elements of the components are showing up. Ex: The title
in my component is blank, rather than "Test Event" as shown in the terminal output.
Inside componentWillMount()
I query my database to populate an array which is then used in renderItem
to populate the FlatList
Terminal output shows an initial render()
with an empty array and then a second render()
with the data.
My guess is that the components are trying to pull from the empty array, thus causing them to be empty. I am confused though because a second call to render()
is being logged, so won't the component be rendered again as well? with the full array?
After doing some research, I think I may need to call forceUpdate()
or something similar to grab the updated data from the array. I have also tried messing around with componentWillReceiveProps()
but I can't get that working either (Source). To be honest, I am not certain on my use of renderItem
, this could be contributing to my problem.
I haven't included EventCard.js
as I am quite certain it is not the source of the error but I can upon request.
18:00:16: RENDER
18:00:16: Array []
18:00:17: RENDER
18:00:17: Array [
18:00:17: Object {
18:00:17: "date": "Fri Dec 07 2018",
18:00:17: "host": "Test host",
18:00:17: "key": "-L4mE_YKKbrvaMVNaA2T",
18:00:17: "location": "",
18:00:17: "time": "08:30",
18:00:17: "title": "Test event",
18:00:17: "volunteersHave": 1,
18:00:17: "volunteersNeed": "20",
18:00:17: },
18:00:17: ]
The relevant file, Dashboard.js
import React from 'react';
import { Button, FlatList, StyleSheet, Text, View} from 'react-native';
import { Icon } from 'react-native-elements';
import EventCard from './EventCard';
import * as firebase from 'firebase';
export default class Dashboard extends React.Component {
constructor(props){
super(props)
this.state = {
eventArr: [],
}
}
componentWillMount = () => {
var tempArr = [];
firebase.database().ref('/events/').once('value').then((snapshot) => {
tempArr = this.snapshotToArray(snapshot);
this.setState({
eventArr: tempArr
});
});
}
snapshotToArray = snapshot => {
var retArr = [];
snapshot.forEach(childSnapshot => {
var item = childSnapshot.val();
item.key = childSnapshot.key;
retArr.push(item);
});
return retArr;
};
render(){
console.log('RENDER')
console.log(this.state.eventArr)
return (
<View>
<FlatList
data={this.state.eventArr}
renderItem={({item}) =>
<EventCard>
title={item.title}
date={item.date}
location={item.location}
rsvp={item.volunteersHave}
time={item.time}
onPress={() => navigate('EventPage')}
</EventCard>}
/>
</View>
);
}
}
//styles omitted
const styles = StyleSheet.create({
});
Upvotes: 2
Views: 1237
Reputation: 2047
You are wrapping the parameters in wrong way
You should try this:
<EventCard
title={item.title}
date={item.date}
location={item.location}
rsvp={item.volunteersHave}
time={item.time}
onPress={() => navigate('EventPage')}
/>
Upvotes: 1
Reputation: 1275
I think your problem is with your renderItem function. Specifically, you have all your props outside of the EventCard
<EventCard>
title={item.title}
date={item.date}
location={item.location}
rsvp={item.volunteersHave}
time={item.time}
onPress={() => navigate('EventPage')}
</EventCard>
Should be
<EventCard
title={item.title}
date={item.date}
location={item.location}
rsvp={item.volunteersHave}
time={item.time}
onPress={() => navigate('EventPage')}>
</EventCard>
Upvotes: 2