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. 封装application类

    <?php  //判断用户是否是通过入口文件访问   if(!defined('ACCESS')){     echo '非法请求';     die;   }   //封装初始化类   cla ...

  3. JSP里比对单选框或复选框的数值而自动打勾

    <table> <tr> <td class="tableleft">状态</td> <td><input typ ...

  4. Android 利用Service实现下载网络图片至sdk卡

    package com.example.myapp5; import android.app.Activity; import android.content.Intent; import andro ...

  5. JavaScript中Date(日期对象),Math对象--学习笔记

    Date对象 1.什么是Date对象? 日期对象可以储存任意一个日期,并且可以精确到毫秒数(1/1000 秒). 语法:var Udate=new Date();  注:初始值为当前时间(当前电脑系统 ...

  6. LDA-math-认识Beta/Dirichlet分布

    http://cos.name/2013/01/lda-math-beta-dirichlet/#more-6953 2. 认识Beta/Dirichlet分布2.1 魔鬼的游戏—认识Beta 分布 ...

  7. RobotFramework 安装配置(二)

    前面已经写了一篇关于RF的安装配置了,那是在做自动化工具调研的时候搭建RF总结的,基于win32的系列软件安装的过程.经过1个月的调研,做成了demo,也大致学RF的使用和python的基础语法,暂时 ...

  8. php setcookie 讲解

    1.setcookie 中 $value 值不能为数组 e.g a.$arr = array('hh','bb');setcookie('username',$arr);这种不会生效的 如果确实要放数 ...

  9. 小结 javascript中的类型检测

    先吐槽一下博客园的编辑器,太不好用了,一旦粘贴个表格进来就会卡死,每次都要用html编辑器写,不爽! 关于javascript的类型检测,早在实习的时候就应该总结,一直拖到现在,当时因为这个问题还出了 ...

  10. html 关于块级元素和行内元素

    常用的行内元素要记住:a.span.img.input.lable.select.strong.textarea 常用的块级元素要记住:div.h1~h6.dl.ul.ol 例如在一个title中,有 ...