前面已经已经讲过一次路由   稍微有些复杂   考虑到有些同学刚接触到   我准备了一个简单的demo   就当自己也顺便复习一下

data.js

 const data = [
{
name: 'Tacos',
description: 'A taco (/ˈtækoʊ/ or /ˈtɑːkoʊ/) is a traditional Mexican dish composed of a corn or wheat tortilla folded or rolled around a filling. A taco can be made with a variety of fillings, including beef, pork, chicken, seafood, vegetables and cheese, allowing for great versatility and variety. A taco is generally eaten without utensils and is often accompanied by garnishes such as salsa, avocado or guacamole, cilantro (coriander), tomatoes, minced meat, onions and lettuce.',
items: [
{ name: 'Carne Asada', price: 7 },
{ name: 'Pollo', price: 6 },
{ name: 'Carnitas', price: 6 }
]
},
{
name: 'Burgers',
description: 'A hamburger (also called a beef burger, hamburger sandwich, burger or hamburg) is a sandwich consisting of one or more cooked patties of ground meat, usually beef, placed inside a sliced bun. Hamburgers are often served with lettuce, bacon, tomato, onion, pickles, cheese and condiments such as mustard, mayonnaise, ketchup, relish, and green chile.',
items: [
{ name: 'Buffalo Bleu', price: 8 },
{ name: 'Bacon', price: 8 },
{ name: 'Mushroom and Swiss', price: 6 }
]
},
{
name: 'Drinks',
description: 'Drinks, or beverages, are liquids intended for human consumption. In addition to basic needs, beverages form part of the culture of human society. Although all beverages, including juice, soft drinks, and carbonated drinks, have some form of water in them, water itself is often not classified as a beverage, and the word beverage has been recurrently defined as not referring to water.',
items: [
{ name: 'Lemonade', price: 3 },
{ name: 'Root Beer', price: 4 },
{ name: 'Iron Port', price: 5 }
]
}
] const dataMap = data.reduce((map,category) => //重点看这里 在干嘛 对es6很有帮助哟
{
category.itemMap =
category.items.reduce((itemMap,item)=>{
itemMap[item.name] = item;
return itemMap
},{})
map[category.name] = category;
return map
},
{}
) exports.getAll = function () {
return data
} exports.lookupCategory = function (name) {
return dataMap[name]
} exports.lookupItem = function (category, item) {
return dataMap[category].itemsMap[item]
}

app.js

 import React from 'react'
import { render } from 'react-dom'
import { browserHistory, Router, Route, Link } from 'react-router' import withExampleBasename from '../withExampleBasename'
import data from './data' import './app.css' const category = ({children,params})=>{
const category = data.lookupCategory(params.category) return (
<div>
<h1>{category.name}</h1>
{children || (
<p>{category.description}</p>
)}
</div>
)
} const CategorySidebar = ({ params }) => {
const category = data.lookupCategory(params.category) return (
<div>
<Link to="/">◀︎ Back</Link> // "/" 根目录 到app组件 app包含什么 往下看
<h2>{category.name} Items</h2>
<ul>
{category.items.map((item, index) => (
<li key={index}>
<Link to={`/category/${category.name}/${item.name}`}>{item.name}</Link> //见下面的route
</li>
))}
</ul>
</div>
)
} const Item = ({ params: { category, item } }) => {
const menuItem = data.lookupItem(category, item) return (
<div>
<h1>{menuItem.name}</h1>
<p>${menuItem.price}</p>
</div>
)
} const Index = () => (
<div>
<h1>Sidebar</h1>
<p>
Routes can have multiple components, so that all portions of your UI
can participate in the routing.
</p>
</div>
) const IndexSidebar = () => (
<div>
<h2>Categories</h2>
<ul>
{data.getAll().map((category, index) => (
<li key={index}>
<Link to={`/category/${category.name}`}>{category.name}</Link>
</li>
))}
</ul>
</div>
) const App = ({ content, sidebar }) => (
<div>
<div className="Sidebar">
{sidebar || <IndexSidebar />}
</div>
<div className="Content">
{content || <Index />}
</div>
</div>
) render((
<Router history={withExampleBasename(browserHistory, __dirname)}>
<Route path="/" component={App}>
<Route path="category/:category" components={{ content: Category, sidebar: CategorySidebar }}> // 一级
<Route path=":item" component={Item} /> //二级
</Route>
</Route>
</Router>
), document.getElementById('example'))

看懂了吧  很简单吧

我们再来看一个例子

 import React from 'react'
import { render } from 'react-dom'
import { browserHistory, Router, Route, Link } from 'react-router' import withExampleBasename from '../withExampleBasename' const User = ({ params: { userID }, location: { query } }) => {
let age = query && query.showAge ? '33' : '' return (
<div className="User">
<h1>User id: {userID}</h1>
{age}
</div>
)
} const App = ({ children }) => (
<div>
<ul>
<li><Link to="/user/bob" activeClassName="active">Bob</Link></li>
<li><Link to={{ pathname: '/user/bob', query: { showAge: true } }} activeClassName="active">Bob With Query Params</Link></li>
<li><Link to="/user/sally" activeClassName="active">Sally</Link></li>
</ul>
{children}
</div>
) render((
<Router history={withExampleBasename(browserHistory, __dirname)}>
<Route path="/" component={App}>
<Route path="user/:userID" component={User} />
</Route>
</Router>
), document.getElementById('example'))

简单吧 找到自信了吗 。。。

最后看一个复杂一点的   当做巩固吧

一个一个来

 import React from 'react'
import { render } from 'react-dom'
import { browserHistory, Router, Route, IndexRoute, Link } from 'react-router' import withExampleBasename from '../withExampleBasename' const PICTURES = [
{ id: 0, src: 'http://placekitten.com/601/601' },
{ id: 1, src: 'http://placekitten.com/610/610' },
{ id: 2, src: 'http://placekitten.com/620/620' }
] const Modal = React.createClass({
styles: {
position: 'fixed',
top: '20%',
right: '20%',
bottom: '20%',
left: '20%',
padding: 20,
boxShadow: '0px 0px 150px 130px rgba(0, 0, 0, 0.5)',
overflow: 'auto',
background: '#fff'
}, render() {
return (
<div style={this.styles}>
<p><Link to={this.props.returnTo}>Back</Link></p> //这里是要返回当前属性的地址
{this.props.children}
</div>
)
}
})
 const App = React.createClass({

   componentWillReceiveProps(nextProps) {  //组件改变时  就是路由切换时
// if we changed routes...
if ((
nextProps.location.key !== this.props.location.key && //经过上面的讲解这里的location.key和state 懂吧
nextProps.location.state &&
nextProps.location.state.modal
)) {
// save the old children (just like animation)
this.previousChildren = this.props.children
}
}, render() {
let { location } = this.props let isModal = (
location.state &&
location.state.modal &&
this.previousChildren
) return (
<div>
<h1>Pinterest Style Routes</h1> <div>
{isModal ?
this.previousChildren :
this.props.children
} {isModal && (
<Modal isOpen={true} returnTo={location.state.returnTo}> // 这里是往回跳转
{this.props.children} //大家要问this,props.children 是指的什么
</Modal>
)}
</div>
</div>
)
}
})

index-route  当app没有定义时  被包含进去

 const Index = React.createClass({
render() {
return (
<div>
<p>
The url `/pictures/:id` can be rendered anywhere in the app as a modal.
Simply put `modal: true` in the location descriptor of the `to` prop.
</p> <p>
Click on an item and see its rendered as a modal, then copy/paste the
url into a different browser window (with a different session, like
Chrome -> Firefox), and see that the image does not render inside the
overlay. One URL, two session dependent screens :D
</p> <div>
{PICTURES.map(picture => (
<Link
key={picture.id}
to={{
pathname: `/pictures/${picture.id}`,
state: { modal: true, returnTo: this.props.location.pathname }
}}
>
<img style={{ margin: 10 }} src={picture.src} height="100" />
</Link>
))}
</div> <p><Link to="/some/123/deep/456/route">Go to some deep route</Link></p> </div>
)
}
})

大家如果还是不很懂   可以看一下index-route  举个例子

 import React from 'react'
import { render } from 'react-dom'
import { Router, Route, hashHistory, IndexRoute } from 'react-router'
import App from './modules/App'
import About from './modules/About'
import Repos from './modules/Repos'
import Repo from './modules/Repo'
import Home from './modules/Home' render((
<Router history={hashHistory}>
<Route path="/" component={App}>
<IndexRoute component={Home}/>
<Route path="/repos" component={Repos}>
<Route path="/repos/:userName/:repoName" component={Repo}/>
</Route>
<Route path="/about" component={About}/>
</Route>
</Router>
), document.getElementById('app'))

看下app.js

 import React from 'react'
import NavLink from './NavLink' export default React.createClass({
render() {
return (
<div>
<h1>React Router Tutorial</h1>
<ul role="nav">
<li><NavLink to="/about">About</NavLink></li>
<li><NavLink to="/repos">Repos</NavLink></li>
</ul>
{this.props.children} //如果一开始访问 根目录/ 其他的字组件还没包含进去 那么。。。 看下面
</div>
)
}
})

home.js

 import React from 'react'

 export default React.createClass({
render() {
return <div>Home</div> //知道了吧 刚才进去 被包含进去的就是home组件 当进行路由跳转的时候
}
})

navLink.js

 // modules/NavLink.js
import React from 'react'
import { Link } from 'react-router' export default React.createClass({
render() {
return <Link {...this.props} activeClassName="active"/>
}
})

repo.js

 import React from 'react'

 export default React.createClass({
render() {
return (
<div>
<h2>{this.props.params.repoName}</h2>
</div>
)
}
})

repos.js

 import React from 'react'
import NavLink from './NavLink' export default React.createClass({
render() {
return (
<div>
<h2>Repos</h2>
<ul>
<li><NavLink to="/repos/reactjs/react-router">React Router</NavLink></li>
<li><NavLink to="/repos/facebook/react">React</NavLink></li>
</ul>
{this.props.children} //repos肯定有子路由
</div>
)
}
})

无关紧要的about.js

 import React from 'react'

 export default React.createClass({
render() {
return <div>About</div>
}
})

最重要的index.js

 import React from 'react'
import { render } from 'react-dom'
import { Router, Route, hashHistory, IndexRoute } from 'react-router'
import App from './modules/App'
import About from './modules/About'
import Repos from './modules/Repos'
import Repo from './modules/Repo'
import Home from './modules/Home' render((
<Router history={hashHistory}>
<Route path="/" component={App}>
<IndexRoute component={Home}/>
<Route path="/repos" component={Repos}>
<Route path="/repos/:userName/:repoName" component={Repo}/>
</Route>
<Route path="/about" component={About}/>
</Route>
</Router>
), document.getElementById('app'))

我们来整理一下

1.进入根目录/  渲染app  下面有home  和  repos(下面又有repo)

2.根据指定路径到不同组件 上面代码中,用户访问/repos时,会先加载App组件,然后在它的内部再加载Repos组件。

今天就这么多  这些基础的东西还是很重要的   话说我最近还真是忙呀

一个屁点大的公司,就我一个前端,还tm什么都要会,静态页面lz一个人些,还要做交互的特效,坑爹的事各种响应式,还非要什么flex布局。。。这还没完呀

而且搞完了lz还要去用redux开发后台页面,现在小公司没一个不坑的,借口都是培养成全站式程序员,但又没实际的培训的技术指导,总监和pm什么的都在家

遥控指挥,你觉得这不是笑话么。。。笑话归笑话,又要开始烧脑了

react路由案例(非常适合入门)的更多相关文章

  1. Laravel初级教程浅显易懂适合入门

    整理了一些Laravel初级教程,浅显易懂,特适合入门,留给刚学习laravel想快速上手有需要的朋友 最适合入门的laravel初级教程(一)序言 最适合入门的laravel初级教程(二)安装使用 ...

  2. Vue-CLI项目路由案例汇总

    0901自我总结 Vue-CLI项目路由案例汇总 router.js import Vue from 'vue' import Router from 'vue-router' import Cour ...

  3. react第十二单元(react路由-使用react-router-dom-认识相关的组件以及组件属性)

    第十二单元(react路由-使用react-router-dom-认识相关的组件以及组件属性) #课程目标 理解路由的原理及应运 理解react-router-dom以及内置的一些组件 合理应用内置组 ...

  4. react第十五单元(react路由的封装,以及路由数据的提取)

    第十五单元(react路由的封装,以及路由数据的提取) #课程目标 熟悉react路由组件及路由传参,封装路由组件能够处理路由表 对多级路由能够实现封装通用路由传递逻辑,实现多级路由的递归传参 对复杂 ...

  5. react第十四单元(react路由-react路由的跳转以及路由信息)

    第十四单元(react路由-react路由的跳转以及路由信息) #课程目标 理解前端单页面应用与多页面应用的优缺点 理解react路由是前端单页面应用的核心 会使用react路由配置前端单页面应用框架 ...

  6. react第十三单元(react路由-react路由的跳转以及路由信息) #课程目标

    第十三单元(react路由-react路由的跳转以及路由信息) #课程目标 熟悉掌握路由的配置 熟悉掌握跳转路由的方式 熟悉掌握路由跳转传参的方式 可以根据对其的理解封装一个类似Vue的router- ...

  7. 适合入门自学服装裁剪滴书(更新ing)

    [♣]适合入门自学服装裁剪滴书(更新ing) [♣]适合入门自学服装裁剪滴书(更新ing) 适合入门自学服装裁剪滴书(更新ing) 来自: 裁缝阿普(不为良匠,便为良医.) 2014-04-06 23 ...

  8. Easyui + asp.net mvc + sqlite 开发教程(录屏)适合入门

    Easyui + asp.net mvc + sqlite 开发教程(录屏)适合入门 第一节: 前言(技术简介) EasyUI 是一套 js的前端框架 利用它可以快速的开发出好看的 前端系统 web ...

  9. React Native 系列(一) -- JS入门知识

    前言 本系列是基于React Native版本号0.44.3写的,最初学习React Native的时候,完全没有接触过React和JS,本文的目的是为了给那些JS和React小白提供一个快速入门,让 ...

随机推荐

  1. 扒皮下GitHub 404的图片层次轴动特效

    今天要克隆的前端特效非常有意思,可以参见GitHub404页面 https://github.com/vajoy/master/index.html 记得之前华为在站酷发布EMUI设计大赛的主页也用了 ...

  2. objective-c(代码块)

    objective-c代码块(block)对写惯C语言的人非常熟悉,就类似一个函数指针,指向一个代码段的首地址: 给出简单例子如下: int main(int argc, const char * a ...

  3. 用Python编写博客导出工具

    用Python编写博客导出工具 罗朝辉 (http://kesalin.github.io/) CC 许可,转载请注明出处   写在前面的话 我在 github 上用 octopress 搭建了个人博 ...

  4. Sqlserver 如何获取每组中的第一条记录

    在日常生活方面,我们经常需要记录一些操作,类似于日志的操作,最后的记录才是有效数据,而且可能它们属于不同的方面.功能下面,从数据库的术语来说,就是查找出每组中的一条数据. 例子 我们要从上面获得的有效 ...

  5. easy-ui 小白进阶史(一):加载数据,easy-ui显示

    作为一个没上过大学,没经过正规培训的96年的小白来说,找工作就没报特别大的希望,大不了找不到在回炉重造,继续学... 终于在海投了200份的简历之后...终于找到了...面试也挺简单的,,,第二天就去 ...

  6. VS 2008 生成操作中各个选项的差别

    近日,在编译C#项目时经常发现有些时候明明代码没错,但就是编译不过,只有选择重新编译或者清理再编译才会不出错,本着求学的态度,搜罗了下VS2008IDE中生成操作的种类以及差别,整理如下:   内容( ...

  7. IOS Socket 02-Socket基础知识

    1. 简介 Socket就是为网络服务提供的一种机制 通信的两端都是Socket 网络通信其实就是Socket间的通信 数据在两个Socket间通过IO传输 2. Socket通信流程图 3. 模拟Q ...

  8. Java基础之面向对象以及其他概念

    一.基础知识:1.JVM.JRE和JDK的区别: JVM(Java Virtual Machine):java虚拟机,用于保证java的跨平台的特性. java语言是跨平台,jvm不是跨平台的. JR ...

  9. js的基本语句和语法

    字符串赋值:把字符串用双引号或单引号引起来,在(js.php)中:二.类型转换;parseint():转整数.parsefloat转小数:强制转换三.运算符表达式1数序运算:加减乘除   %取余:2逻 ...

  10. JDBC操作数据库,第一:jsp插入mysql数据库,坎坷摸索分享

    JSP连接数据库,坎坷摸索了好久,现在终于做好了,分享一下,希望对更多热爱编程学习的人有所帮助!!!谢谢 第一:首先准备的就是已经安装好Mysql,这里不做多叙述,百度可以做到. 然后在mysql数据 ...