ReactJs入門思路小指南
React是怎么搞的?
React中,把一切東西都看成組件,而且所有組件有其狀態。
什么是狀態?簡單來說,一個組件有多種有限的狀態,但同時只能是一種狀態,不過條件處罰就會變成另一種狀態。學術上叫有限狀態機。
具體可以參考阮老師的這篇http://www.ruanyifeng.com/blog/2013/09/finite-state_machine_for_javascript.html
從評論這個組件說起,評論組件整體叫做CommentBox,這個父組件有兩個子組件:CommentList和CommentForm,CommentList中又包含Comment這個小組件。
從CommentBox開始
先來看看最大的CommentBox,創建一個叫CommentBox組件,然后可以用這個名字作為標簽名實例化。里面包含兩個子組件。數據我們可以從屬性上傳進去。
var data = [ {author: "Pete Hunt", text: "This is one comment"}, {author: "Jordan Walke", text: "This is another comment"} ]; var CommentBox = React.createClass({ render: function() { return (<div className="commentBox"> <CommentList data="this.props.data"/> </div>
);
} });
var CommentList = React.createClass({ render: function() { var commentNodes = this.props.data.map(function (comment) { return ( <Comment author={comment.author}> {comment.text} </Comment> ); }); return ( <div className="commentList"> {commentNodes} </div> ); } }); React.render( <CommentBox data={data}/>, document.getElementById('content') );</pre>
在React中,最重要的兩個點之一就是這個props,可以通過組件的屬性來傳遞數據。上面的代碼就可以展現出評論的列表。
獲取服務器的數據
那么如何獲取服務器數據?這個時候就需要引入一開始說的狀態state。
var data = [ {author: "Pete Hunt", text: "This is one comment"}, {author: "Jordan Walke", text: "This is another comment"} ]; var CommentBox = React.createClass({ getInitialState: function() { return {data: []}; }, loadCommentsFromServer: function() { $.ajax({ url: this.props.url, dataType: 'json', success: function(data) { this.setState({data: data}); }.bind(this), error: function(xhr, status, err) { console.error(this.props.url, status, err.toString()); }.bind(this) }); }, componentDidMount: function() { this.loadCommentsFromServer(); setInterval(this.loadCommentsFromServer, this.props.pollInterval); }, render: function() { return (<div className="commentBox"> <CommentList data={this.state.data} /> </div>
);
} });
var CommentList = React.createClass({
render: function() { var commentNodes = this.props.data.map(function (comment) { return ( <Comment author={comment.author}> {comment.text} </Comment> ); }); return ( <div className="commentList"> {commentNodes} </div> ); } }); React.render( <CommentBox url="comments.json" pollInterval={2000}/>, document.getElementById('content') );</pre>
通過getInitialState設置初始化的狀態,然后通過React提供的用來渲染的componentDidMount來輪詢數據,這樣就可以獲取服務端的數據。
怎么提交數據呢?
也是通過設置props和state來設置組件狀態和數據。
var CommentForm = React.createClass({ handleSubmit: function(e) { e.preventDefault(); var author = this.refs.author.getDOMNode().value.trim(); var text = this.refs.text.getDOMNode().value.trim(); if (!text || !author) { return; } this.refs.author.getDOMNode().value = ''; this.refs.text.getDOMNode().value = ''; this.props.onCommentSubmit({author: author, text: text}); return; }, render: function() { return ( <form className="commentForm"> <input type="text" placeholder="Your name" /> <input type="text" placeholder="Say something..." /> <input type="submit" value="Post" /> </form> ); } }); var CommentBox = React.createClass({ handleCommentSubmit: function(comment) { $.ajax({ url: this.props.url, dataType: 'json', type: 'POST', data: comment, success: function(data) { this.setState({data: data}); }.bind(this), error: function(xhr, status, err) { console.error(this.props.url, status, err.toString()); }.bind(this) }); }, render: function() { return ( <div className="commentBox"> <h1>Comments</h1> <CommentList data={this.state.data} /> <CommentForm onCommentSubmit={this.handleCommentSubmit} /> </div> ); } });
來自:http://segmentfault.com/blog/fakefish/1190000002449277