【问题标题】:How do I reorder/sort Firebase data in React App?如何在 React App 中重新排序/排序 Firebase 数据?
【发布时间】:2020-01-30 03:24:54
【问题描述】:

所以我在这里坐以待毙,我需要帮助重新排序数据集。这是我的反应组件的基础。这是我正在做的个人项目,然后在工作中将其重用于实际项目。其基础来自关于 React + Firebase 的 YouTube 系列:https://www.youtube.com/playlist?list=PL4cUxeGkcC9iWstfXntcj8f-dFZ4UtlN3

理想情况下,我想要一组按 asc 或 desc 对数据进行排序的按钮。并且可能依赖于其他一些东西。

import React, { Component } from 'react';
import ShopList from '../shops/ShopList.js';
import { Helmet } from 'react-helmet';
import { connect } from 'react-redux';
import { firestoreConnect } from 'react-redux-firebase';
import { compose } from 'redux';


class Dashboard extends Component {
  reOrder = (e) => {
    e.preventDefault();
    console.log("button works!");
  }

  render() {
    const { shops } = this.props;

    return(

      <div className="dashboard container">
      <Helmet>
          <title>Dashboard | Indianapolis Coffee Guide</title>
      </Helmet>
        <div className="row">
            <div className="col-sm-6">
              <button className="btn btn-primary" onClick={this.reOrder}>Reorder</button>
            </div>
        </div>
        <div className="row">
            <ShopList shops={shops} />
        </div>
      </div>
    )
  }
}

const mapStateToProps = (state) => {
  return {
    shops: state.firestore.ordered.coffeeShops,
    auth: state.firebase.auth
  }
}

export default compose(
  connect(mapStateToProps),
  firestoreConnect([
    { collection: 'coffeeShops', orderBy: ['shopName', 'asc']}
  ])
)(Dashboard)

数据现在正在以 orderBy 结尾,但我需要能够更新它...

感谢您的帮助!

【问题讨论】:

    标签: reactjs firebase firebase-realtime-database google-cloud-firestore create-react-app


    【解决方案1】:

    我不确定它会起作用,因为我从未使用过react-redux-firebase,但在我看来,您可以创建一个 redux 操作来设置 redux 商店中的订购方向。像这样:

    import React, { Component } from 'react';
    import ShopList from '../shops/ShopList.js';
    import { Helmet } from 'react-helmet';
    import { connect } from 'react-redux';
    import { firestoreConnect } from 'react-redux-firebase';
    import { compose } from 'redux';
    
    
    class Dashboard extends Component {
      reOrder = (e) => {
        e.preventDefault();
        console.log("button works!");
        const orderDirection = this.props.orderDirection === 'asc' ? 'desc' : 'asc';
        this.props.dispatch(ReOderAction(orderDirection));
      }
    
      render() {
        const { shops } = this.props;
    
        return(
    
          <div className="dashboard container">
          <Helmet>
              <title>Dashboard | Indianapolis Coffee Guide</title>
          </Helmet>
            <div className="row">
                <div className="col-sm-6">
                  <button className="btn btn-primary" onClick={this.reOrder}>Reorder</button>
                </div>
            </div>
            <div className="row">
                <ShopList shops={shops} />
            </div>
          </div>
        )
      }
    }
    
    const mapStateToProps = (state) => {
      return {
        shops: state.firestore.ordered.coffeeShops,
        auth: state.firebase.auth,
        orderDirection: state.anything.orderDirection,
      }
    }
    
    export default compose(
      connect(mapStateToProps),
      firestoreConnect([
        { collection: 'coffeeShops', orderBy: ['shopName', orderDirection]}
      ])
    )(Dashboard)
    

    请告诉我它是否有效...

    【讨论】:

    • 所以我将您的 const 更改为 let 并且它运行了,但它不会更新呈现的数据。单击按钮后,我记录了 orderDirection,它更改了应有的值,但数据本身没有更新。有什么想法吗?另外,就像我说的,我只是按照该教程进行操作。因此,如果您知道另一种方式(re:更好的方式),您将获得数据,我也很乐意尝试一下。谢谢!
    • 是的,我刚刚更新了我的答案。您可以在父组件上传递prop(比如说orderDirection)并更改父组件的状态,或者您可以专门创建一个redux 操作来设置它。像这样你的组件肯定会更新。
    • 由于未定义 ReOrderAction 而无法构建?
    • 是的,我知道。正如我所说,您需要创建一个 Redux 操作...您知道该怎么做吗?如果您不这样做,为什么不尝试将道具从其父组件传递给 组件?
    • 也许,在这种情况下,您不应该尝试使用 redux。您必须编写一些样板文件,这可能有点乏味。但是如果你真的想使用它,那么考虑看一些教程。一个好的开始就在这里:youtube.com/…
    猜你喜欢
    • 2018-02-06
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 2023-03-03
    • 1970-01-01
    • 2021-12-07
    相关资源
    最近更新 更多