【问题标题】:React-Native/Firebase Insert and sort by timestampReact-Native/Firebase 插入并按时间戳排序
【发布时间】:2017-08-16 21:38:43
【问题描述】:

我最近一直在使用 react-native,因为我正在构建一个基本应用程序,您可以在其中拥有一个演出列表,每个演出都有一个关联的日期,该日期作为时间戳插入到 firebase。

我想检查一下,但是按日期对数组进行排序以便列表显示最新的最好方法是什么?

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 };
};

谢谢!

【问题讨论】:

    标签: javascript firebase react-native ecmascript-6


    【解决方案1】:

    让 firebase 执行此操作,使用 firebase 查询

    例如: 说你想展示最近的 10 场演出

    firebase.database().ref('gigs/').orderByChild('date').limitToLast(10);
    

    检查文档的 limitToFirst 和 limitToLast 部分 https://firebase.google.com/docs/database/web/lists-of-data

    【讨论】:

    • 如何降序排序?
    猜你喜欢
    • 2016-10-19
    • 2021-12-03
    • 2019-02-08
    • 2012-10-18
    • 2018-10-22
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多