Reputation: 621
I've been working in react-native recently as I'm building a basic app where you can have a list of gigs, each having an associated date which is inserted into firebase as a timestamp.
I wanted to check but what's the best approach to sorting an array by date so that the list displays with the most recent please?
componentWillMount() {
this.props.gigsFetch();
this.createDataSource(this.props);
}
componentWillReceiveProps(nextProps) {
this.createDataSource(nextProps);
}
createDataSource({ gigs }) {
const ds = new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2
});
this.dataSource = ds.cloneWithRows(gigs);
}
const mapStateToProps = state => {
const sortedGigs = _.sortBy(state.gigs, 'date');
const gigs = _.map(sortedGigs, (val, uid) => {
return { ...val, uid };
});
return { gigs };
};
Thank you!
Upvotes: 0
Views: 2137
Reputation: 1181
let firebase do this, use the firebase query
for ex: say you want to show the latest 10 gigs
firebase.database().ref('gigs/').orderByChild('date').limitToLast(10);
check the limitToFirst and limitToLast parts of the documentation https://firebase.google.com/docs/database/web/lists-of-data
Upvotes: 2