javascript - 如何在 React 中对 jsx 中的元素进行排序?

标签 javascript reactjs react-redux

我有一个应用程序,用户可以 CRUD 线程,主要结构如下所示,

export default function Dashboard(){
    return(
        <Fragment>
            <CreateBoard />
            <BoardList />
        </Fragment>
     )
}

Dashboard 将在 App.js 中调用。
董事会列表

import { getBoards, deleteBoard } from "../../actions/boards"

export class BoardList extends Component {
    static propTypes = {
        boards: PropTypes.array.isRequired,
        getBoards: PropTypes.func.isRequired,
        deleteBoard: PropTypes.func.isRequired,
    }
    componentDidMount() {
        this.props.getBoards();
    }

    render(){
        return (
            <Fragment>
                <h2>Boards</h2>
                <table className="table table-striped">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Author</th>
                            <th>Title</th>
                            <th>Created</th>
                            <th>Updated</th>
                            <th />
                        </tr>
                    </thead>
                    <tbody>  
                   // this sort does not work
                    {this.props.boards.length > 0 && this.props.boards.sort((boardA, boardB) => boardA.id < boardB.id)
                        .map(board => (
                            <tr key={board.id}>
                                <td>{board.id}</td>
                                <td>{board.author}</td>
                                <td>{board.title}</td>
                                <td>{board.created}</td>
                                <td>{board.updated}</td>
                                <td>
                                    <button 
                                      className="btn btn-danger btn-sm"
                                      onClick={this.props.deleteBoard.bind(this, board.id)}
                                      >
                                      Delete</button>
                                </td>
                            </tr>
                        ))}
                    </tbody>
                </table>
            </Fragment>
        )
    }
}

const mapStateToProps = state => ({
    boards: state.boards.boards
})

export default connect(mapStateToProps, {getBoards, deleteBoard})(BoardList)

即使我对它进行排序,它总是对我的列表后代顺序进行排序(较新的帖子位于顶部)。如何修复它而不每次都渲染它?

最佳答案

sort 可能会更干净在返回函数中而不是 JSX 中。此外,您还需要将 Prop 克隆到可以排序的新数组中。

  render() {
    const sortedBoard = [...this.props.boards].sort((boardA, boardB) => {
      return boardA.id > boardB.id;
    });
    const sortedRows = sortedBoard.map(board => {
      return (
        <tr key={board.id}>
          <td>{board.id}</td>
          <td>{board.author}</td>
          <td>{board.title}</td>
          <td>{board.created}</td>
          <td>{board.updated}</td>
          <td>
            <button
              className="btn btn-danger btn-sm"
              onClick={this.props.deleteBoard.bind(this, board.id)}
            >
              Delete
            </button>
          </td>
        </tr>
      );
    });

    return (
      <Fragment>
        <h2>Boards</h2>
        <table className="table table-striped">
          <thead>
            <tr>
              <th>ID</th>
              <th>Author</th>
              <th>Title</th>
              <th>Created</th>
              <th>Updated</th>
              <th />
            </tr>
          </thead>
          <tbody>{this.props.boards.length && { sortedRows }}</tbody>
        </table>
      </Fragment>
    );
  }

注意sort可能有点棘手...

let a = [ '1', '2', '10', '3' ];
a.sort();
// [ '1', '10', '2', '3' ]

let b = [1, 2, 10, 3];
b.sort((x,y) => { return x-y });
// [ 1, 2, 3, 10 ]

关于javascript - 如何在 React 中对 jsx 中的元素进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60861055/

相关文章:

javascript - 文本框 JavaScript 中的 Control+退格键

node.js - React 中删除请求

javascript - 需要修改 HTML 重定向脚本帮助以显示重定向 url 而不是重定向

javascript - 在 iframe 中设置输入文本并提交

javascript - (仍然)对 javascript 闭包、ajax 和返回值更加困惑

javascript - 如何将 javascript 代码添加到reactJS 文件中?

reactjs - IE11 错误 : TypeError: non-array objects must have a [Symbol. iterator]() 方法

javascript - React es5 与 es6

javascript - 调度 Action 触发,但 redux 存储不更新

javascript - 如何修复错误: Could not find router reducer in state tree,它必须安装在 "router"下