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. C++Builder 解决绘图闪动问题

    使用双缓冲 Form->DoubleBuffered = true; Panel->DoubleBuffered = true;

  2. [原创] hadoop学习笔记:卸载和安装jdk

    一,卸载jdk 1.确定jdk版本 #rpm -qa  | grep jak 可能的结果: java-1.7.0-openjdk-1.7.0.75-2.5.4.2.el7_0.x86_64 java- ...

  3. shell学习笔记(1)-变量

    1.shell中的变量可以自定义,shell中使用变量时用$ name="shero"echo "hi ${name}" root@shero-virtual- ...

  4. mysql bin log日志

    装mysql,运行一段时间后,在mysql目录下出现一堆类似mysql-bin.000***,从mysql-bin.000001开始一直排列下来,而且占用了大量硬盘空间,高达几十个G. 对于这些超大空 ...

  5. 部署ganglia3.7

    环境 centOS6.6 gmetad节点关闭iptable gmetad和httpd只需要在一台节点安装,gmond需要在每台节点上安装. 一.安装epel源 sudo wget http://do ...

  6. 2016-9-6 批量给文件名的前面加上“igeek_高薪就业” 2、 利用FileInputStream和FileOutputStream复制文件

    在此只列出典型题目,有的题目扫一眼就有代码的不去浪费时间了,想要完整题目的评论留邮箱,看到就发.持续更新中... 1.批量给文件名的前面加上“igeek_高薪就业” package com.work; ...

  7. 使用sudo执行命令的时候提示找不到命令

    事出有因, 源自使用源码编译 nginx , 在 /usr/local/sbin/ 目录下创建了nginx 启动的符号链接 , 执行 sudo nginx 的时候提示找不到命令, 但是使用普通用户身份 ...

  8. zw版_Halcon-delphi系列教程_卫星航拍精确打击目标自动识别

    <zw版·Halcon-delphi系列原创教程> 卫星航拍精确打击目标自动识别 这几天,俄罗斯空军在叙利亚大显神威,美军有意见了,说俄罗斯是狂轰滥炸,不是精确打击.      不过,战斗 ...

  9. DB2 Unload 的时候遇到B37-04

    B37-04的问题是每个Mainframer首先会遇到的问题,在Unload的时候最大的可能性是Extend次数到16次了,这时候只要加大primary或secondary就可以了,我最常用的是pri ...

  10. 一、Java基础--01

    Java基础测试题分析 第一题是关于基本的算法知识,这个很有必要去掌握以下,在学校也经常听老师们说找工作比试面试会出一些这方面的知识,我拿到的第一题是关于排序的,虽然很简单,但是我还是要提醒一下基础不 ...