Display posts in descending posted order

前端 未结 18 1294
南方客
南方客 2020-11-22 11:47

I\'m trying to test out Firebase to allow users to post comments using push. I want to display the data I retrieve with the following;

fbl.child         


        
18条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 12:08

    I'm using ReactFire for easy Firebase integration.

    Basically, it helps me storing the datas into the component state, as an array. Then, all I have to use is the reverse() function (read more)

    Here is how I achieve this :

    import React, { Component, PropTypes } from 'react';
    import ReactMixin from 'react-mixin';
    import ReactFireMixin from 'reactfire';
    
    import Firebase from '../../../utils/firebaseUtils'; // Firebase.initializeApp(config);
    
    @ReactMixin.decorate(ReactFireMixin)
    export default class Add extends Component {
        constructor(args) {
            super(args);
    
            this.state = {
                articles: []
            };
        }
    
        componentWillMount() {
            let ref = Firebase.database().ref('articles').orderByChild('insertDate').limitToLast(10);
            this.bindAsArray(ref, 'articles'); // bind retrieved data to this.state.articles
        }
    
        render() {
            return (
                
    { this.state.articles.reverse().map(function(article) { return
    {article.title}
    }) }
    ); } }

提交回复
热议问题