tutorial.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. let data = [
  2. { id: 1, author: "Pete Hunt", text: "This is one comment" },
  3. { id: 2, author: "Jordan Walke", text: "This is *another* comment" }
  4. ];
  5. let Comment = React.createClass({
  6. rawMarkup: function () {
  7. let rawMarkup = marked(this.props.children.toString(), { sanitize: true });
  8. return { __html: rawMarkup };
  9. },
  10. render: function () {
  11. return (
  12. <div className="comment">
  13. <h2 className="commentAuthor">
  14. {this.props.author}
  15. </h2>
  16. <span dangerouslySetInnerHTML={this.rawMarkup()} />
  17. </div>
  18. );
  19. }
  20. });
  21. let CommentList = React.createClass({
  22. render: function () {
  23. let commentNodes = this.props.data.map(function (comment) {
  24. return (
  25. <Comment author={comment.author} key={comment.id}>
  26. {comment.text}
  27. </Comment>
  28. );
  29. });
  30. return (
  31. <div className="commentList">
  32. {commentNodes}
  33. </div>
  34. );
  35. }
  36. });
  37. let CommentForm = React.createClass({
  38. render: function () {
  39. return (
  40. <div className="commentForm">
  41. Hello, world! I am a CommentForm.
  42. </div>
  43. );
  44. }
  45. });
  46. let CommentBox = React.createClass({
  47. render: function () {
  48. return (
  49. <div className="commentBox">
  50. <h1>Comments</h1>
  51. <CommentList data={this.props.data} />
  52. <CommentForm />
  53. </div>
  54. );
  55. }
  56. });
  57. ReactDOM.render(
  58. <CommentBox url="/api/comments" />,
  59. document.getElementById("content")
  60. );