Last active
September 15, 2016 09:23
-
-
Save dgellow/8455754 to your computer and use it in GitHub Desktop.
React.js tutorial.
http://facebook.github.io/react/docs/tutorial.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <title>Hello react</title> | |
| <link rel="stylesheet" type="text/css" href="http://cdn.jsdelivr.net/foundation/5.0.2/css/foundation.css"> | |
| <script src="http://cdnjs.cloudflare.com/ajax/libs/react/0.8.0/react.min.js"></script> | |
| <script src="http://code.jquery.com/jquery-1.10.0.min.js"></script> | |
| <script src="http://cdnjs.cloudflare.com/ajax/libs/showdown/0.3.1/showdown.min.js"></script> | |
| </head> | |
| <body> | |
| <div id="content"></div> | |
| <script type="text/javascript"> | |
| var _ = null; | |
| dom = React.DOM | |
| var CommentBox = React.createClass({ | |
| getInitialState: function(){ | |
| return {data: []}; | |
| }, | |
| loadCommentsFromServer: function(){ | |
| $.ajax({ | |
| url: 'comments.json', | |
| dataType: 'json', | |
| success: function(data) { | |
| this.setState({data: data}); | |
| }.bind(this), | |
| error: function(xhr, status, err) { | |
| console.error('comments.json', status, err.toString()); | |
| }.bind(this) | |
| }); | |
| }, | |
| handleCommentSubmit: function(comment) { | |
| var comments = this.state.data; | |
| var newComments = comments.concat([comment]); | |
| this.setState({data: newComments}); | |
| $.ajax({ | |
| url: this.props.url, | |
| type: 'POST', | |
| data: comment, | |
| success: function(data) { | |
| this.setState({data: data}); | |
| }.bind(this) | |
| }); | |
| }, | |
| componentWillMount: function(){ | |
| this.loadCommentsFromServer(); | |
| setInterval(this.loadCommentsFromServer, this.props.pollInterval); | |
| }, | |
| render: function(){ | |
| return ( | |
| dom.div( | |
| // Attributes | |
| {className: 'commentBox'}, | |
| // Children | |
| dom.h1(_, 'Comments'), | |
| CommentList({data: this.state.data}), | |
| CommentForm({onCommentSubmit: this.handleCommentSubmit}) | |
| ) | |
| ); | |
| } | |
| }); | |
| var CommentList = React.createClass({ | |
| render: function(){ | |
| var commentNodes = this.props.data.map(function(comment) { | |
| return Comment({author: comment.author}, comment.text); | |
| }); | |
| return ( | |
| dom.div( | |
| {className: 'commentList'}, | |
| commentNodes | |
| ) | |
| ); | |
| } | |
| }); | |
| var CommentForm = React.createClass({ | |
| handleSubmit: function(event){ | |
| var author = this.refs.author.getDOMNode().value.trim(); | |
| var text = this.refs.text.getDOMNode().value.trim(); | |
| this.props.onCommentSubmit({author: author, text: text}); | |
| this.refs.author.getDOMNode().value = ''; | |
| this.refs.text.getDOMNode().value = ''; | |
| preventDefault(event); | |
| }, | |
| render: function(){ | |
| return dom.form( | |
| {className: 'commentForm', onSubmit: this.handleSubmit}, | |
| dom.input({type: 'text', placeholder: 'Your name', | |
| ref: 'author'}), | |
| dom.input({type: 'text', placeholder: 'Say something...', | |
| ref: 'text'}), | |
| dom.input({type: 'submit', value: 'Post'}) | |
| ); | |
| } | |
| }); | |
| var converter = new Showdown.converter(); // Markdown | |
| var Comment = React.createClass({ | |
| render: function(){ | |
| var rawMarkup = converter.makeHtml(this.props.children.toString()); | |
| return dom.div( | |
| {className: 'comment'}, | |
| dom.h2({className: 'commentAuthor'}, this.props.author), | |
| dom.span({dangerouslySetInnerHTML: {__html: rawMarkup}}) | |
| ); | |
| } | |
| }); | |
| // Rendering | |
| React.renderComponent( | |
| CommentBox({url: 'comments.json', pollInterval: 2000}), | |
| document.getElementById('content') | |
| ); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment