index

<!DOCTYPE html>
<html> <head>
<meta charset="utf-8">
<!-- Disable browser cache -->
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
<title>Project One</title>
<link rel="stylesheet" href="vendor/semantic-ui/semantic.min.css" />
<link rel="stylesheet" href="style.css" />
<script src="vendor/babel-core-5.8.25.js"></script>
<script src="vendor/react.js"></script>
<script src="vendor/react-dom.js"></script>
</head> <body>
<div class="main ui text container">
<h1 class="ui dividing centered header">Popular Products</h1>
<div id="content"></div>
</div>
<script src="./data.js"></script>
<script type="text/babel" src="./app.js"></script>
<script src="http://localhost:35729/livereload.js?snipver=1"></script>
<!-- Delete the line below to get started. -->
<!-- <script type="text/babel" src="./app-complete.js"></script> -->
</body> </html>

data:

window.Data = (function () {
function generateVoteCount() {
return Math.floor((Math.random() * ) + );
} const data = [
{
id: ,
title: 'Yellow Pail',
description: 'On-demand sand castle construction expertise.',
url: '#',
votes: generateVoteCount(),
submitter_avatar_url: 'images/avatars/daniel.jpg',
product_image_url: 'images/products/image-aqua.png',
},
{
id: ,
title: 'Supermajority: The Fantasy Congress League',
description: 'Earn points when your favorite politicians pass legislation.',
url: '#',
votes: generateVoteCount(),
submitter_avatar_url: 'images/avatars/kristy.png',
product_image_url: 'images/products/image-rose.png',
},
{
id: ,
title: 'Tinfoild: Tailored tinfoil hats',
description: 'We already have your measurements and shipping address.',
url: '#',
votes: generateVoteCount(),
submitter_avatar_url: 'images/avatars/veronika.jpg',
product_image_url: 'images/products/image-steel.png',
},
{
id: ,
title: 'Haught or Naught',
description: 'High-minded or absent-minded? You decide.',
url: '#',
votes: generateVoteCount(),
submitter_avatar_url: 'images/avatars/molly.png',
product_image_url: 'images/products/image-yellow.png',
},
]; return data;
})();

app:

'use strict'
/**
*产品列表
**/ /* eslint-disable no-undef */ const ProductList = React.createClass({
// getInitialState: function () {
// return {
// products: [],
// };
// },
// componentDidMount: function () {
// this.updateState();
// },
// updateState: function () {
// const products = Data.sort((a, b) => {
// return b.votes - a.votes;
// });
// this.setState({ products: products });
// },
// handleUpVote: function (productId) {
// Data.forEach((el) => {
// if (el.id === productId) {
// el.votes = el.votes + 1;
// return;
// }
// });
// this.updateState();
// }, getInitialState: function() {
return {
products:[],
};
},
componentDidMount:function (){
this.updateState();
},
updateState:function(){
const products = Data.sort(function(a,b){
return b.votes - a.votes;
});
this.setState({products:products})
},
handleUpVote: function (productId) {
for(var item in Data){
if(Data[item].id == productId){
Data[item].votes = Data[item].votes +;
continue;
}
}
this.updateState();
}, render: function () {
const products = this.state.products.map((product) => {
return (
<Product
key={product.id}
id={product.id}
title={product.title}
description={product.description}
url={product.url}
votes={product.votes}
submitter={product.submitter}
submitter_avatar_url={product.submitter_avatar_url}
product_image_url={product.product_image_url}
onVote={this.handleUpVote}
/>
);
});
return (
<div className='ui items'>
{products}
</div>
);
},
}); const Product = React.createClass({
handleUpVote: function () {
this.props.onVote(this.props.id);
},
render: function () {
return (
<div className='item'>
<div className='image'>
<img src={this.props.product_image_url} />
</div>
<div className='middle aligned content'>
<div className='ui grid'>
<div className='three wide column'>
<div className='ui basic center aligned segment'>
<a onClick={this.handleUpVote}>
<i className='large caret up icon'></i>
</a>
<p><b>{this.props.votes}</b></p>
</div>
</div>
<div className='twelve wide column'>
<div className='header'>
<a href={this.props.url}>
{this.props.title}
</a>
</div>
<div className='meta'>
<span></span>
</div>
<div className='description'>
<p>{this.props.description}</p>
</div>
<div className='extra'>
<span>Submitted by:</span>
<img
className='ui avatar image'
src={this.props.submitter_avatar_url}
/>
</div>
</div>
</div>
</div>
</div>
);
},
}); ReactDOM.render(
<ProductList />,
document.getElementById('content')
);

reactjs入门到实战(十)----one-first_app的更多相关文章

  1. reactjs入门到实战(七)---- React的组件的生命周期

    React的组件的生命周期有三个状态分别是:挂载(生产组件示例化.准备挂载到页面.挂载到页面).更新(更新值.更新DOM).和卸载(卸载后). >>>其他     getInitia ...

  2. reactjs入门到实战(九)----ajax的应用

    利用外部的jquery: <script type="text/babel"> } }, componentDidMount:function(){ ]['value' ...

  3. reactjs入门到实战(八)----表单组件的使用

    表单组件支持几个受用户交互影响的属性: value,用于 <input>.<textarea> 组件. checked,用于类型为 checkbox 或者 radio 的 &l ...

  4. reactjs入门到实战(六)---- ReactJS组件API详解

    全局的api 1.React.createClass 创建一个组件类,并作出定义.组件实现了 render() 方法,该方法返回一个子级.该子级可能包含很深的子级结构.组件与标准原型类的不同之处在于, ...

  5. reactjs入门到实战(五)---- props详解

    1>>>基础的props使用     不可修改父属性    getDefaultProps   对于外界/父组件的属性值,无法直接修改,它是只读的. <script type= ...

  6. reactjs入门到实战(四)---- state详解

    this.props 表示那些一旦定义,就不再改变的特性,而 this.state 是会随着用户互动而产生变化的特性. 组件免不了要与用户互动,React 的一大创新,就是将组件看成是一个状态机,一开 ...

  7. reactjs入门到实战(三)---- 组件详解

    owner  >>> 传递 props this >>>是默认指向组件本身 key>>>不能没有,在复用的情况下 组件:例子 <!-- 输入 ...

  8. reactjs入门到实战(二)---- jxs详解

    >>>如何转换    JSX transformer   Babel    官网:http://babeljs.io/   里面有一个可以看转换的测试器,es6什么的也可以应用: 注 ...

  9. reactjs入门到实战(一)---- hello world例子

    React 起源于 Facebook 的内部项目,因为该公司对市场上所有 JavaScript MVC 框架,都不满意,就决定自己写一套,用来架设 Instagram 的网站.做出来以后,发现这套东西 ...

随机推荐

  1. UVa10023手动开大数平方算法

    题目链接:UVa 10023 import java.math.BigInteger; import java.util.Scanner; public class Main { public sta ...

  2. nyist 518 取球游戏

    http://acm.nyist.net/JudgeOnline/problem.php?pid=518 取球游戏 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 今 ...

  3. DO.NET操作数据库

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. Eclipse下配置C++开发环境(转)

    1. 首先确保你的电脑上已经安装了Java,如果没有,或者不确定,请到官网上下载并安装,网址如下(这一步我就不详述了): http://www.java.com/zh_CN/   2. 到官网上下载并 ...

  5. poj: 3006

    简单题 #include <iostream> #include <stdio.h> #include <string> #include <stack> ...

  6. JSon_零基础_008_将JSon格式的"数组"字符串转换为List集合

    将JSon格式的"数组"字符串转换为List集合. 应用此技术从一个json对象字符串格式中得到一个java对应的对象. JSONObject是一个“name.values”集合, ...

  7. sql查询所有表以及表名的模糊查询

    --1.查看所有表名:select name from sysobjects where type='U'--2.查找包含用户的表名,可通过以下SQL语句实现, Select * From sysob ...

  8. 夺命雷公狗---DEDECMS----29dedecms热门大片的完成

    我们要完成的是热门大片的显示,如下所示: 热门大片,这一般都是按照浏览量高的放前面的而已,我们先来查查手册,如下所示: 这里发现我们的arclist标签里面的orderby和orderway完全可以实 ...

  9. PAT乙级 1013. 数素数 (20)

    1013. 数素数 (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 令Pi表示第i个素数.现任给两个正整 ...

  10. DB2 表空间和缓冲池

    简介 对于刚涉足 DB2 领域的 DBA 或未来的 DBA 而言,新数据库的设计和性能选择可能会很令人困惑.在本文中,我们将讨论 DBA 要做出重要选择的两个方面:表空间和缓冲池.表空间和缓冲池的设计 ...