Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <script src="//fb.me/react-0.13.1.js"></script>
</head>
<body>
  <h1>Comments</h1>
  <div id="react_example"></div>
</body>
</html>
 
var CommentBox = React.createClass({
  getInitialState: function() {
    return {
      // list limit
      limit: 3,
      // state of the button
      showMore: true
    };
  },
  showMore: function() {
    this.setState({
      // do not show the button any more
      showMore: false, 
      // set limit to current count
      // what happens if the comments increase?
      limit: this.props.comments.length
    });
  },
  renderButton: function () {
    // show button only if state.showMore set to true
    if (!this.state.showMore) return null;
    return (
      <button onClick={this.showMore}>ShowMore</button>
    );
  },
            render: function() {
      // copy up to limit entries from the props.comments
      var cls = this.props.comments.slice(0,this.state.limit);
      return (
        <div className="commentBox">
        <ul>
          { cls.map(function (c) { return (<li key={c}>{c}</li>); }) }
        </ul>
        { this.renderButton() }
        </div>
    );
  }
});
React.render(
  <CommentBox comments={[1,2,3,4]}/>,
  document.getElementById('react_example')
);
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers