tutorial.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. let Comment = React.createClass({
  2. rawMarkup: function () {
  3. let rawMarkup = marked(this.props.children.toString(), { sanitize: true });
  4. return { __html: rawMarkup };
  5. },
  6. render: function () {
  7. return (
  8. <div className="comment">
  9. <h2 className="commentAuthor">
  10. {this.props.author}
  11. </h2>
  12. <span dangerouslySetInnerHTML={this.rawMarkup()} />
  13. </div>
  14. );
  15. }
  16. });
  17. let CommentList = React.createClass({
  18. render: function () {
  19. let commentNodes = this.props.data.map(function (comment) {
  20. return (
  21. <Comment author={comment.author} key={comment.id}>
  22. {comment.text}
  23. </Comment>
  24. );
  25. });
  26. return (
  27. <div className="commentList">
  28. {commentNodes}
  29. </div>
  30. );
  31. }
  32. });
  33. let CommentForm = React.createClass({
  34. render: function () {
  35. return (
  36. <div className="commentForm">
  37. Hello, world! I am a CommentForm.
  38. </div>
  39. );
  40. }
  41. });
  42. let CommentBox = React.createClass({
  43. getInitialState: function () {
  44. return { data: [] };
  45. },
  46. componentDidMount: function () {
  47. $.ajax({
  48. url: this.props.url,
  49. dataType: "json",
  50. cache: false,
  51. success: function (result) {
  52. console.log('Received frop api: ', result);
  53. this.setState({ data: result });
  54. }.bind(this),
  55. error: function (xhr, status, err) {
  56. console.error(this.props.url, status, err.toString());
  57. }.bind(this)
  58. });
  59. },
  60. render: function () {
  61. return (
  62. <div className="commentBox">
  63. <h1>Comments</h1>
  64. <CommentList data={this.state.data} />
  65. <CommentForm />
  66. </div>
  67. );
  68. }
  69. });
  70. ReactDOM.render(
  71. <CommentBox url="/api/comments" />,
  72. document.getElementById("content")
  73. );