我最近一直在使用 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 };
};
谢谢!
最佳答案
让 firebase 执行此操作,使用 firebase 查询
例如: 假设您想展示最新 10 场演出
firebase.database().ref('gigs/').orderByChild('date').limitToLast(10);
检查文档的 limitToFirst 和 limitToLast 部分 https://firebase.google.com/docs/database/web/lists-of-data
关于javascript - React-Native/Firebase 插入并按时间戳排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42991199/