浅谈React实现评论框(二)

http://blog.csdn.net/zhouziyu2011/article/details/70502495实现了简单的评论框,但实现的只是在代码里直接插入评论数据。

实际情况是,评论数据应该是来自服务器的 JSON 数据,现在不考虑服务器,直接将 JSON 数据写在代码中:

// 模拟服务器返回的JSON数据
var data = [
	{id: 1, author: "Alice", text: "This is Alice's comment"},
	{id: 2, author: "Bruce", text: "This is Bruce's comment"}
];
var CommentBox = React.createClass({
	render: function() {
		return (
			<div className="commentBox">
				<h1>CommentBox</h1>
				<CommentList data={this.props.data} />
				<CommentForm />
			</div>
		);
	}
});
var CommentList = React.createClass({
	render: function() {
		var commentNodes = this.props.data.map(function(comment) {
			return <Comment author={comment.author} key={comment.id}>{comment.text}</Comment>
		});
		return (
			<div className="commentList">
				{commentNodes}
			</div>
		);
	}
});
var Comment = React.createClass({
	render: function() {
		return (
			<div className="comment">
				<h3>{this.props.author}</h3>
				{this.props.children}
			</div>
		);
	}
});
var CommentForm = React.createClass({
	render: function() {
		return (
			<div className="commentForm">
				CommentForm
			</div>
		);
	}
});
ReactDOM.render(
	<CommentBox data={data}/>,
	document.getElementById('commentBox')
);

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值