Skip to content

Instantly share code, notes, and snippets.

@jordwalke
Last active September 10, 2016 16:27
Show Gist options
  • Save jordwalke/6350319 to your computer and use it in GitHub Desktop.
Save jordwalke/6350319 to your computer and use it in GitHub Desktop.
ReactJS: JavaScript just like you've always done it.
// Renders your top ten most followed friends/followers, `filter`ing only your favorites,
// putting a star on all verified accounts.
// When using the React API, any time any of the data changes through state
// updates, the `render` function will always be satisfied. If new followers
// or friends are added, the UI automatically updates. If the ranking by
// followCount changes, the ordering of rendering automatically moves
// nodes to their proper place in the ordering. If a person becomes "verified"
// they automatically get a star. Any of this could happen at any point in
// time and the UI will always be updated, and React computes the minimum set
// of DOM mutations needed to make it happen.
// No bindings to automatically setup. No dependency tracking to configure.
// Just JavaScript. Just regular arrays. Just Functions.
var PeopleList = React.createClass({
render: function() {
var friends = this.props.friends;
var followers = this.props.followers;
return div({className: 'list'},
friends.concat(followers)
.filter(function(person) {return person.isFavorite;})
.sort(function(one, two) {return one.followCount - two.followCount;})
.slice(0, 10)
.map(function(person) {
return div({className: person.isVerified ? 'star' : 'gray'});
})
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment