这是我在官方文档上看到的,功能是实现(具体是什么,请往下看)

以下是json数据:

 [
{category: "Sporting Goods", price: "$49.99", stocked: true, name: "Football"},
{category: "Sporting Goods", price: "$9.99", stocked: true, name: "Baseball"},
{category: "Sporting Goods", price: "$29.99", stocked: false, name: "Basketball"},
{category: "Electronics", price: "$99.99", stocked: true, name: "iPod Touch"},
{category: "Electronics", price: "$399.99", stocked: false, name: "iPhone 5"},
{category: "Electronics", price: "$199.99", stocked: true, name: "Nexus 7"}
];

显示红色的物品,是没有库存的(stocked:false),如$29.99的basketball和$399.99的iphone5。

在我看来react的结构就像‘包饺子’,一层夹着一层,可以看一下上图的几个边框,不同颜色代表着不同区域划分。

1.(orange): contains the entirety of the example 外边的黄色区域,包含整个例子。

2.(blue): receives all user input  里面一层的蓝色框框,度用户的输入进行serach。

3.(green): displays and filters the data collection based onuser input   绿色框展示用户筛选的物品信息。

4. (turquoise): displays a heading for each category  显示产品种类,这里是sproting goods和electronics。

5.(red): displays a row for each product  红色框,每件物品。

 //这是结构的最外一层  它里面包含searchBar和producTable两个组件
var FilterableProductTable = React.createClass({
render: function() {
return (
<div>
<SearchBar />
<ProductTable products={this.props.products} />
</div>
);
}
}); var PRODUCTS = [
{category: 'Sporting Goods', price: '$49.99', stocked: true, name: 'Football'},
{category: 'Sporting Goods', price: '$9.99', stocked: true, name: 'Baseball'},
{category: 'Sporting Goods', price: '$29.99', stocked: false, name: 'Basketball'},
{category: 'Electronics', price: '$99.99', stocked: true, name: 'iPod Touch'},
{category: 'Electronics', price: '$399.99', stocked: false, name: 'iPhone 5'},
{category: 'Electronics', price: '$199.99', stocked: true, name: 'Nexus 7'}
];
//这里的prop属性定义了product 引用字上面的json数据
ReactDOM.render(
<FilterableProductTable products={PRODUCTS} />,
document.getElementById('container')
); //这是最简单的搜索框组件
var SearchBar = React.createClass({
render: function() {
return (
<form>
<input type="text" placeholder="Search..." />
<p>
<input type="checkbox" />
{' '}
Only show products in stock
</p>
</form>
);
}
}); //关键的来了 这个productTable里面还包含了一些组件
var ProductTable = React.createClass({
render: function() {
var rows = [];
var lastCategory = null;
//遍历json数据
this.props.products.forEach(function(product) {
if (product.category !== lastCategory) { //注意这里的判断,并不是每回都要把类别加一次
//这里有一个产品种类的组件
rows.push(<ProductCategoryRow category={product.category} key={product.category} />);
}
//还有一个产品行的组件
rows.push(<ProductRow product={product} key={product.name} />);
lastCategory = product.category;
});
return (
<table>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>{rows}</tbody>
</table>
);
}
}); //category在上面可以找到
var ProductCategoryRow = React.createClass({
render: function() {
return (<tr><th colSpan="2">{this.props.category}</th></tr>);
}
}); var ProductRow = React.createClass({
render: function() {
//如果没有货存 红色字体显示
var name = this.props.product.stocked ?
this.props.product.name :
<span style={{color: 'red'}}>
{this.props.product.name}
</span>;
return (
<tr>
<td>{name}</td>
<td>{this.props.product.price}</td>
</tr>
);
}
});

就这样玩了吗?还没有,以上还没实现搜索和过滤功能。

 var ProductCategoryRow = React.createClass({
render: function() {
return (<tr><th colSpan="2">{this.props.category}</th></tr>);
}
}); var ProductRow = React.createClass({
render: function() {
var name = this.props.product.stocked ?
this.props.product.name :
<span style={{color: 'red'}}>
{this.props.product.name}
</span>;
return (
<tr>
<td>{name}</td>
<td>{this.props.product.price}</td>
</tr>
);
}
}); //这里都没变 //注意这里有改动
var ProductTable = React.createClass({
render: function() {
var rows = [];
var lastCategory = null;
this.props.products.forEach(function(product) {
//用于过滤产品(用户输入的关键字和库存)
if (product.name.indexOf(this.props.filterText) === -1 || (!product.stocked && this.props.inStockOnly)) { //注意此处的逻辑
return;
}
if (product.category !== lastCategory) {
rows.push(<ProductCategoryRow category={product.category} key={product.category} />);
}
rows.push(<ProductRow product={product} key={product.name} />);
lastCategory = product.category;
}.bind(this)); //由于函数里面用到了this 这里bind一下
return (
<table>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>{rows}</tbody>
</table>
);
}
}); //serachBar现在膨胀了 这么多
var SearchBar = React.createClass({
handleChange: function() {
//上面肯定有个函数 肯定在父组件上
this.props.onUserInput(
this.refs.filterTextInput.value,
this.refs.inStockOnlyInput.checked //注意这里的ref用法
);
},
render: function() {
return (
<form>
<input
type="text"
placeholder="Search..."
value={this.props.filterText}
ref="filterTextInput"
onChange={this.handleChange} //文本改变回调事件 它的作用是什么? 下面有答案
/>
<p>
<input
type="checkbox"
checked={this.props.inStockOnly}
ref="inStockOnlyInput"
onChange={this.handleChange} //checkbox的判断
/>
{' '}
Only show products in stock
</p>
</form>
);
}
}); //关键的来了 这个里面就是饺子皮
var FilterableProductTable = React.createClass({
getInitialState: function() {
return {
filterText: '',
inStockOnly: false
};
}, //这里初始化的state 文本为空 默认checkbox=false handleUserInput: function(filterText, inStockOnly) {
this.setState({
filterText: filterText,
inStockOnly: inStockOnly
});
}, render: function() {
return (
<div>
<SearchBar
//prop定义的的位置 可以对照着看
filterText={this.state.filterText}
inStockOnly={this.state.inStockOnly}
onUserInput={this.handleUserInput} //这个函数用于改变初始state
/>
//prop定义的位置 要注意这里的ProductTable里面的filterText和instockOnly 是随着serachBar的输入变动的
<ProductTable
products={this.props.products}
filterText={this.state.filterText}
inStockOnly={this.state.inStockOnly}
/>
</div>
);
}
}); var PRODUCTS = [
{category: 'Sporting Goods', price: '$49.99', stocked: true, name: 'Football'},
{category: 'Sporting Goods', price: '$9.99', stocked: true, name: 'Baseball'},
{category: 'Sporting Goods', price: '$29.99', stocked: false, name: 'Basketball'},
{category: 'Electronics', price: '$99.99', stocked: true, name: 'iPod Touch'},
{category: 'Electronics', price: '$399.99', stocked: false, name: 'iPhone 5'},
{category: 'Electronics', price: '$199.99', stocked: true, name: 'Nexus 7'}
]; ReactDOM.render(
<FilterableProductTable products={PRODUCTS} />,
document.getElementById('container')
);

好了,代码量并不大,但是这里面的逻辑和每层之间的关系确实有点搞人呀。。。希望大家看懂了react想表达的是什么

react经典进阶demo的更多相关文章

  1. 【视频合集】极客时间 react实战进阶45讲 【更新中】

    https://up2.v.sharedaka.com/video/ochvq0AVfpa71A24bmugS5EewhFM1553702519936.mp4 01 React出现的历史背景及特性介绍 ...

  2. React Native官方DEMO

    官方给我们提供了UIExplorer项目,这里边包含React Native的基本所有组件的使用介绍和方法. 运行官方DEMO步骤如下 安装react native环境 React Native项目源 ...

  3. React问答小demo

    在学习react初期,看了一些视频和资料,react基础知识差不多学完,跟着网上的一个教程,做了一个小型的问答demo. 需求看图说: 1.点击"添加"按钮,显示问题输入表单,再次 ...

  4. 助你了解react的小demo

    React是个啥 React 是一个用于构建用户界面的 JAVASCRIPT 库. React主要用于构建UI,很多人认为 React 是 MVC 中的 V(视图). React 起源于 Facebo ...

  5. 一个 react 小的 demo

    一.搭建开发环境: webpack构建工具. 新建一个文件夹(login),进入根目录, 1.输入命令:cnpm init,生成了一个package.json文件,这是一个标准的npm说明文件,里面蕴 ...

  6. react native进阶

    一.前沿||潜心修心,学无止尽.生活如此,coding亦然.本人鸟窝,一只正在求职的鸟.联系我可以直接微信:jkxx123321 二.项目总结 **||**文章参考资料:1.  http://blog ...

  7. React Native八大Demo

    参考资料:http://www.cnblogs.com/shaoting/p/7148240.html 下一个项目公司也打算使用react native.大致看了下原型设计,写几个小demo先试试水. ...

  8. react context toggleButton demo

    //toggleButton demo: //code: //1.Appb.js: import React from 'react'; import {ThemeContext, themes} f ...

  9. react login page demo

    1. login form import React from "react"; import {Row, Col} from "antd"; import { ...

随机推荐

  1. 解决Ubuntu Server 12.04 在Hyper-v 2012 R2中不能使用动态内存的问题

    前言 全新Hyper-v 2012 R2终于开始支持在Linux的VPS中使用动态内存,可以大大优化服务器的资源分配,小弟我兴奋不已,于是抽空时间赶紧升级到 2012 R2,好好整理一番内存分配,不过 ...

  2. apache httpclient cache 实现可缓存的http客户端

    这里的cache storage 采用ehcache,而不是默认的内存式的cache storage.采用ehcache可以将内容缓存到磁盘上. maven <dependency> &l ...

  3. Java虚拟机4:内存溢出

    堆溢出 Java堆唯一的作用就是存储对象实例,只要保证不断创建对象并且对象不被回收,那么对象数量达到最大堆容量限制后就会产生内存溢出异常了.所以测试的时候把堆的大小固定住并且让堆不可扩展即可.测试代码 ...

  4. Zookeeper--Zookeeper是什么

    Google的三篇论文影响了很多很多人,也影响了很多很多系统.这三篇论文一直是分布式领域传阅的经典.根据MapReduce,于是我们有了Hadoop:根据GFS,于是我们有了HDFS:根据BigTab ...

  5. npm穿墙

    GWF 很给力,很多东西都能墙掉,但是把 npm 也纳入黑名单,不知道 GWFer 是怎么想的.FQ翻了好多年了,原理其实也挺简单的,proxy 嘛! » 方法一 A) 国内源,http://cnpm ...

  6. 【译】什么导致了Context泄露:Handler&内部类

    思考下面代码 public class SampleActivity extends Activity { private final Handler mLeakyHandler = new Hand ...

  7. java线程与并发(一)

    有好几个月没写博客了,各种破事儿忙完,决定继续写博客,恰好最近想了解下有关Java并发的一些知识,所以就准备这一段时间,用零碎的时间多记录一点有关并发的知识.希望这次能一直坚持下去. 想了解并发,必须 ...

  8. MVC5:使用Ajax和HTML5实现文件上传功能

    引言 在实际编程中,经常遇到实现文件上传并显示上传进度的功能,基于此目的,本文就为大家介绍不使用flash 或任何上传文件的插件来实现带有进度显示的文件上传功能. 基本功能:实现带有进度条的文件上传功 ...

  9. IOS Animation-CAKeyframeAnimation例子(简单动画实现)

    在阅读本文之前,可以看看 CABasicAnimation的例子 也可以看看IOS Animation-CABasicAnimation.CAKeyframeAnimation详解&区别&am ...

  10. jetty

    相关的文章太多了,我只按照自己的意思做简单总结. 参见: http://www.cnblogs.com/duanxz/p/3154982.html http://www.cnblogs.com/win ...