JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://fb.me/react-with-addons-0.9.0.js"></script>
<script src="http://fb.me/JSXTransformer-0.9.0.js"></script>
<script src="http://rawgithub.com/Marak/Faker.js/master/MinFaker.js"></script>
<script src="http://fb.me/react-js-fiddle-integration.js"></script>
<div id="root"></div>
CSS
.list {
background: lightgray;
}
.star {
color: yellow;
}
JavaScript 1.7
/**
* ReactJS: JavaScript like you've always done it.
*
* This example renders your top ten most followed friends/followers, `filter`ing
* only your favorites, and putting a star on all verified accounts.
*
* With ReactJS, any time your data changes, the UI is always brought up to date
* automatically. If friends length changes, or followCount - it always shows what
* `render` describes.
*/
var div = React.DOM.div;
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 two.followCount - one.followCount;})
.slice(0, 10)
.map(function(person) {
return div(
{className: person.isVerified ? 'star' : 'gray'},
person.isVerified ? person.name.toUpperCase() : person.name, ' (',
person.followCount, ' followers)'
);
})
);
}
});
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
// Generate fake data
function makeRandom(n) {
var people = [];
for (var i = 0; i < n; i++) {
people.push({
name: Faker.Name.findName(),
isFavorite: Math.random() < .2,
isVerified: Math.random() < .2,
followCount: Math.floor(Math.random() * 500)
});
}
return people;
}
function incrementFollowCount(people) {
people.forEach(function(person) {
person.followCount += Math.floor(Math.random() * 5);
});
}
var friends = makeRandom(5000);
var followers = makeRandom(5000);
setInterval(function() {
incrementFollowCount(friends);
incrementFollowCount(followers);
React.renderComponent(PeopleList({friends: friends, followers: followers}), document.getElementById('root'));
}, 1000);
...