简体   繁体   中英

React-Native/Firebase Insert and sort by timestamp

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM