React开发项目例子
一、需求
1.分析:用react开发一个类似bootstrap4中的card组件http://v4-alpha.getbootstrap.com/components/card/,界面类似如下:

2.确定发目标:

3.确定开发顺序

4.开发流程介绍

测试采用TDD

二、代码
1.Card.js
var React = require('react/addons');
var Card = React.createClass({
getInitialState: function() {
return this.props.content;
},
handleClick: function() {
this.setState({
blocks : [{
title: "Allen Iverson(已关注)",
subtitle: "PG",
text: "Cool player",
links: [{
url:"http://www.163.com",
name:"链接1"
}, {
url:"http://www.g.cn",
name:"链接2"
}
]
}]
})
},
render: function(){
var content = this.state;
var blocks = [];
for(var i = 0; i < content.blocks.length; i++){
var block = content.blocks[i];
var links = [];
for(var i = 0; i < block.links.length; i++){
links.push(<a onClick={this.handleClick} className="card-link" href={block.links[i].url}>{block.links[i].name}</a>);
}
blocks.push(<div className="card-block">
<h4 className="card-title">{block.title}</h4>
<h6 className="card-subtitle">{block.subtitle}</h6>
<p className="card-text">{block.text}</p>
<p>
{links}
</p>
</div>
);
}
var listGroup = [];
for(var i = 0; i < content.listGroup.length; i++){
listGroup.push(<li className="list-group-item">{content.listGroup[i]}</li>);
}
var option = this.props.option ? "card text-xs-" + this.props.option : "card";
return <div className="container-fluid">
<div className="row">
<div className="col-sm-4">
<div className={option}>
<div className="card-header">{content.header}</div>
<img className="card-img-top" src={content.imgTop.url} alt={content.imgTop.alt}></img>
{blocks}
<ul className="list-group list-group-flush">
{listGroup}
</ul>
<img className="card-img-bottom" src={content.imgBottom.url} alt={content.imgBottom.alt}></img>
<div className="card-footer">{content.footer}</div>
</div>
</div>
</div>
</div>
}
})
module.exports = Card
2.test.jsx
var React = require('react/addons');
var jasmineReact = require('jasmine-react-helpers');
var TestUtils = React.addons.TestUtils;
var Card = require('./Card.jsx');
describe('Card component', function(){
var card;
var content;
beforeEach(function(){
//渲染
var blocks = [
{
title: "Allen Iverson",
subtitle: "PG",
text: "Cool player",
links: [{
url:"http://www.163.com",
name:"链接1"
}, {
url:"http://www.g.cn",
name:"链接2"
}
]
}
];
var header = "76ers";
var footer = "mvp";
var listGroup = ["艾弗森1996年6月26日被费城76人队选中,成为NBA状元秀,绰号答案(The Answer)","场均出战41.1分钟,获得26.7分、6.2次助攻和2.2次抢断"];
var imgTop = {
url: "http://a1.hoopchina.com.cn/attachment/Day_100424/43_3842044_665ae051136b4b8.jpg",
alt: "dribble"
};
var imgBottom = {
url: "http://www.onlinedown.net/bigsoftimg/androidimg/260000/255860_0.jpg",
alt: "crossover"
}
var content = {
blocks: blocks,
header: header,
footer: footer,
listGroup: listGroup,
imgBottom: imgBottom,
imgTop: imgTop
}
card = TestUtils.renderIntoDocument(<Card content={content} option="center"></Card>);
})
afterEach(function(){
React.unmountComponentAtNode(React.findDOMNode(card))
})
it('should exist', function(){
expect(!!React.findDOMNode(card)).toBe(true)
//card = TestUtils.renderIntoDocument(<Card content={content}></Card>);
//expect(React.findDOMNode(card).textContent).toContain('Hello world')
});
it('should have correct structure', function(){
//测试
//card = TestUtils.renderIntoDocument(<Card content={content}></Card>);
var content = React.findDOMNode(card).textContent;
expect(content).toContain("Allen");
expect(content).toContain("76ers");
expect(content).toContain("mvp");
expect(content).toContain("艾弗森");
expect(React.findDOMNode(card).getElementsByTagName("img")[0].alt).toContain("dribble");
expect(React.findDOMNode(card).getElementsByTagName("img")[1].alt).toContain("crossover");
});
it('should have correct style', function() {
var cardBox = React.findDOMNode(card).getElementsByClassName("card");
expect(!!cardBox.length).toBe(true);
});
it('should correctly use options', function() {
var cardBox = React.findDOMNode(card).getElementsByClassName("text-xs-center");
expect(!!cardBox.length).toBe(true);
});
it('should be response', function() {
TestUtils.Simulate.click(React.findDOMNode(card).getElementsByTagName("a")[0]);
var content = React.findDOMNode(card).textContent;
expect(content).toContain("已关注");
});
})
3.show.jsx
var React = require('react/addons');
var Card = require('./Card.jsx');
var blocks = [
{
title: "Allen Iverson",
subtitle: "PG",
text: "Cool player",
links: [{
url:"http://www.163.com",
name:"链接1"
}, {
url:"http://www.g.cn",
name:"链接2"
}
]
}
];
var header = "76ers";
var footer = "mvp";
var listGroup = ["艾弗森1996年6月26日被费城76人队选中,成为NBA状元秀,绰号答案(The Answer)","场均出战41.1分钟,获得26.7分、6.2次助攻和2.2次抢断"];
var imgTop = {
url: "http://a1.hoopchina.com.cn/attachment/Day_100424/43_3842044_665ae051136b4b8.jpg",
alt: "dribble"
};
var imgBottom = {
url: "http://www.onlinedown.net/bigsoftimg/androidimg/260000/255860_0.jpg",
alt: "crossover"
}
var content = {
blocks: blocks,
header: header,
footer: footer,
listGroup: listGroup,
imgBottom: imgBottom,
imgTop: imgTop
}
React.render(<Card content={content} option="right"></Card>, document.body);
四、运行结果(bootstrap的css无效果)

源码:http://files.cnblogs.com/files/shamgod/BootstrapCard.zip
React开发项目例子的更多相关文章
- react实战项目开发(2) react几个重要概念以及JSX语法
前言 前面我们已经学习了利用官方脚手架搭建一套可以应用在生产环境下的React开发环境.那么今天这篇文章主要先了解几个react重要的概念,以及讲解本文的重要知识JSX语法 React重要概念 [思想 ...
- Expo大作战(三)--针对已经开发过react native项目开发人员有针对性的介绍了expo,expo的局限性,开发时项目选型注意点等
简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...
- react 开发 PC 端项目(一)项目环境搭建 及 处理 IE8 兼容问题
步骤一:项目环境搭建 首先,你不应该使用 React v15 或更高版本.使用仍然支持 IE8 的 React v0.14 即可. 技术选型: 1.react@0.14 2.bootstrap3 3. ...
- react 前端项目技术选型、开发工具、周边生态
react 前端项目技术选型.开发工具.周边生态 声明:这不是一篇介绍 React 基础知识的文章,需要熟悉 React 相关知识 主架构:react, react-router, redux, re ...
- 【腾讯Bugly干货分享】React Native项目实战总结
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/577e16a7640ad7b4682c64a7 “8小时内拼工作,8小时外拼成长 ...
- React Native 项目运行在 Web 浏览器上面
React Native 的出现,让前端工程师拥有了使用 JavaScript 编写原生 APP 的能力.相比之前的 Web app 来说,对于性能和用户体验提升了非常多. 但是 React Nati ...
- 用Inferno代替React开发高性能响应式WEB应用
什么是Inferno Inferno可以看做是React的另一个精简.高性能实现.它的使用方式跟React基本相同,无论是JSX语法.组件的建立.组件的生命周期,还是与Redux或Mobx的配合.路由 ...
- Immutable.js 以及在 react+redux 项目中的实践
来自一位美团大牛的分享,相信可以帮助到你. 原文链接:https://juejin.im/post/5948985ea0bb9f006bed7472?utm_source=tuicool&ut ...
- React开发实时聊天招聘工具 -第一章
第一章 课程道学 6个页面 弱化css Antd-mobile作为组件库 Redux 状态管理 React-Router 路由 Axios异步请求 后端Express框架 Socket.io 数据库: ...
随机推荐
- Thinkphp整合最新Ueditor编辑器
说到最新的富文本编辑器的确不少(ckeditor.fkeditor.ueditor),这些富文本编辑器如果单独使用基本上很方便,不需要做额外的配置,只要把官方的插件下载下来放到一个web容器中,看看 ...
- setEllipsize(TruncateAt where)
void android.widget.TextView.setEllipsize(TruncateAt where) public void setEllipsize (TextUtils.Trun ...
- win7 64位下jboss配置
1.下载Jboss7 下载地址:http://www.jboss.org/jbossas/downloads/ 2.解压到一个目录,如D:\Working,最终路径是D:\Working\jboss- ...
- 每日一“酷”之difflib
介绍:difflib 比较序列.该模块包含一些用来计算和处理序列直接差异的工具.她对于比较文本尤其用,其中包含的函数可以使用多种常用差异格式生成报告. 测试数据(splitlines()按行划分为序列 ...
- 【F#】 入门代码
找下感觉: 语法和go 如出一辙, 都是erlang派的语言 在 vs 中我没有找到自动缩进的快捷键 github上的F#代码也相对较少 // 在 http://fsharp.org 上了解有关 F# ...
- 3144:[HNOI2013]切糕 - BZOJ
题目描述 Description 经过千辛万苦小 A 得到了一块切糕,切糕的形状是长方体,小 A 打算拦腰将切糕切成两半分给小 B.出于美观考虑,小 A 希望切面能尽量光滑且和谐.于是她找到你,希望你 ...
- ViewController 优化
解决问题:部分复杂页面的Controller过于庞大,不利于维护与复用: 复杂的页面大多是基于tableview的页面.复杂页面的代码大致可分为两部分(复杂的View布局用Nib实现的话,一般大家都是 ...
- 【BZOJ】【1115】【POI2009】石子游戏KAM
博弈论 这个题……一看就觉得很捉急啊= =肿么办? 灵光一现:差分一下~ 那么我们看一下差分以后,从第 i 堆中拿走 k 个石子变成了:a[i]-=k; a[i+1]+=k; 嗯这就转化成了阶梯博弈! ...
- Java多线程——<二>将任务交给线程,线程声明及启动
一.任务和线程 <thinking in java>中专门有一小节中对线程和任务两个概念进行了具体的区分,这也恰好说明任务和线程是有区别的. 正如前文所提到的,任务只是一段代码,一段要达成 ...
- centos6.5安装vmware-tools
一.问题描述 为什么使用vmware-tools:传文件方便,可以从主机中直接拖拽文件到centos中(但实际上我们可以用:WinSCP):鼠标切换方便,鼠标指到什么地方,表名当前正在操作哪个环境(但 ...