我正在尝试通过编写类似于体育名册的小型UI来学习React概念,特别是re:状态和动态UI.我已经包含了下面的代码,整个app + visual在http://codepen.io/emkk/pen/dGYXJO.这个应用程序基本上从我之前定义的玩家对象阵列创建玩家卡.
我想在点击按钮时实现播放器卡的排序.我创建了一个
呈现所述按钮的组件.我会附加事件监听器,但不知道如何在我的
组件中反映出来.我已经尝试了许多不同的方法,this.state
但似乎无法让这个工作.所以,任何有关实施分类或一般建议的帮助都会受到很大的帮助!
class ProfileCard extends React.Component { render() { return (); } } 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 () } } 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 (Sort by
AgeHeightName); } }; React.render(2012 Olympic Men's Basketball Team
Average Age: {avgAge} years old
Average Height: 6 ft 7 in ({avgHeights} m)
, document.getElementById('container') );
解:
让我在前往此途中的另一件事是我失去了访问权限this.setState
,不断收到this.setState is a not a function
错误.使用ES6箭头函数来词法绑定this
我的处理程序函数虽然救了我.
class ProfileCard extends React.Component { render() { return (); } } 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 () } } 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 (Sort by
AgeHeightNamePositionNumberClub); } }; ReactDOM.render(2012 Olympic Men's Basketball Team
Average Age: {avgAge} years old
Average Height: 6 ft 7 in ({avgHeight} m)
, document.getElementById('container') );
Naisheel Ver.. 13
附加的功能,每一个在
它调用父功能上的点击this.props.sortBy()
class Sort extends React.Component { sort(field){ this.props.sortBy(field); } render() { return () } }Sort by
AgeHeightName
通过这个父功能sortBy
为props
您的
组件.
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
.直接渲染数组,而不在
组件内部应用任何排序.
附加的功能,每一个在
它调用父功能上的点击this.props.sortBy()
class Sort extends React.Component { sort(field){ this.props.sortBy(field); } render() { return () } }Sort by
AgeHeightName
通过这个父功能sortBy
为props
您的
组件.
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
.直接渲染数组,而不在
组件内部应用任何排序.