当前位置:  开发笔记 > 编程语言 > 正文

React.js - 实现组件的排序

如何解决《React.js-实现组件的排序》经验,为你挑选了1个好方法。

我正在尝试通过编写类似于体育名册的小型UI来学习React概念,特别是re:状态和动态UI.我已经包含了下面的代码,整个app + visual在http://codepen.io/emkk/pen/dGYXJO.这个应用程序基本上从我之前定义的玩家对象阵列创建玩家卡.

我想在点击按钮时实现播放器卡的排序.我创建了一个呈现所述按钮的组件.我会附加事件监听器,但不知道如何在我的组件中反映出来.我已经尝试了许多不同的方法,this.state但似乎无法让这个工作.所以,任何有关实施分类或一般建议的帮助都会受到很大的帮助!

class ProfileCard extends React.Component {
  render() {
    return (
      
{this.props.player.Name}
  • {this.props.player.Name}
  • {this.props.player.position}, #{this.props.player.number}
  • {this.props.player.Club}
  • {this.props.player.Height} ({this.props.player.Meters} m)
  • {this.props.player.Age} years old
); } } class Roster extends React.Component { render() { // Store sorted version of data // Where I'd implement selected sorting var sorted = this.props.players.sort(); /* * Create player cards from JSON collection */ var cards = []; sorted.forEach(function(player) { if (player.year > 2000) { cards.push(); } }); return (
{cards}
); } } class Sort extends React.Component { render() { return (

Sort by

Age
Height
Name
) } } class SortablePlayerTable extends React.Component { render() { /* * Prefix some interestings stats * before roster */ var ages = [], heights = []; this.props.players.forEach(function(player) { if (player.year > 2000) { ages.push(player.Age); heights.push(player.Meters); } }); var avgAge = (ages.reduce( (a, b) => a + b) / 12).toPrecision(3); var avgHeights = (heights.reduce( (a, b) => a + b) / 12).toPrecision(3); // Return page with stats data and Roster return (

2012 Olympic Men's Basketball Team

Average Age: {avgAge} years old

Average Height: 6 ft 7 in ({avgHeights} m)

); } }; React.render( , document.getElementById('container') );

解:

让我在前往此途中的另一件事是我失去了访问权限this.setState,不断收到this.setState is a not a function错误.使用ES6箭头函数来词法绑定this我的处理程序函数虽然救了我.

class ProfileCard extends React.Component {
  render() {
    return (
      
{this.props.player.Name}
  • {this.props.player.Name}
  • {this.props.player.position}, #{this.props.player.number}
  • {this.props.player.Club}
  • {this.props.player.Height} ({this.props.player.Meters} m)
  • {this.props.player.Age} years old
); } } class Roster extends React.Component { render() { // Create player cards from sorted, dynamic JSON collection var cards = []; this.props.players.forEach(function(player) { if (player.year > 2000) { cards.push(); } }); return (
{cards}
); } } class Sort extends React.Component { sortRoster(field){ var players = this.props.players; this.props.sortRosterStateBy(field, players); } render() { return (

Sort by

Age
Height
Name
Position
Number
Club
) } } class SortablePlayerTable extends React.Component { state = { 'players': this.props.players // default state } sortRosterStateBy = (field, players) => { // Sorting ... var sortedPlayers = players.sort( (a, b) => { if (a[field] > b[field]) { return 1; } if (a[field] < b[field]) { return -1; } return 0; }); // Then call setState this.setState({'players': sortedPlayers}); } render() { // Prefix some interestings stats before roster var ages = [], heights = []; this.props.players.forEach(function(player) { if (player.year > 2000) { ages.push(player.Age); heights.push(player.Meters); } }); var avgAge = (ages.reduce( (a, b) => a + b) / 12).toPrecision(3); var avgHeight = (heights.reduce( (a, b) => a + b) / 12).toPrecision(3); // Return page with stats data and Roster return (

2012 Olympic Men's Basketball Team

Average Age: {avgAge} years old

Average Height: 6 ft 7 in ({avgHeight} m)

); } }; ReactDOM.render( , document.getElementById('container') );

Naisheel Ver.. 13

附加的功能,每一个

它调用父功能上的点击this.props.sortBy()

class Sort extends React.Component {
  sort(field){
    this.props.sortBy(field);
  }
  render() {
    return (
      

Sort by

Age
Height
Name
) } }

通过这个父功能sortByprops您的组件.

class SortablePlayerTable extends React.Component {
  state = {
   players: [] // default state
  }
  sortBy(field){
    // Your sorting algorithm here
    // it should push the sorted value by field in array called sortedPlayers 
    // Then call setState
    this.setState({
      players: sortedPlayers
    });
  }
  render() {
    // calculate stats
    return (
      
{/* some JSX */}
); } };

现在,已排序的数组将可用于您的组件this.props.players.直接渲染数组,而不在组件内部应用任何排序.



1> Naisheel Ver..:

附加的功能,每一个

它调用父功能上的点击this.props.sortBy()

class Sort extends React.Component {
  sort(field){
    this.props.sortBy(field);
  }
  render() {
    return (
      

Sort by

Age
Height
Name
) } }

通过这个父功能sortByprops您的组件.

class SortablePlayerTable extends React.Component {
  state = {
   players: [] // default state
  }
  sortBy(field){
    // Your sorting algorithm here
    // it should push the sorted value by field in array called sortedPlayers 
    // Then call setState
    this.setState({
      players: sortedPlayers
    });
  }
  render() {
    // calculate stats
    return (
      
{/* some JSX */}
); } };

现在,已排序的数组将可用于您的组件this.props.players.直接渲染数组,而不在组件内部应用任何排序.

推荐阅读
依然-狠幸福
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有