react-route 版本 0.13.x 与 版本1.0 的改变

Importing

新的 Router 组件是顶层模块的属性。

// v0.13.x
var Router = require('react-router');
var Route = Router.Route;

// v1.0
var ReactRouter = require('react-router');
var Router = ReactRouter.Router;
var Route = ReactRouter.Route;

// or using ES Modules
import { Router, Route } from 'react-router';

Rendering

// v0.13.x
Router.run(routes, (Handler) => {
  render(<Handler/>, el);
})

// v1.0
render(<Router>{routes}</Router>, el)

// looks more like this:
render((
  <Router>
    <Route path="/" component={App}/>
  </Router>
), el);

// or if you'd rather
render(<Router routes={routes}/>, el)

Locations

Locations 现在被称为 histories(可发射位置)。可以从 history package引入,而不是 react router。

// v0.13.x
Router.run(routes, Router.BrowserHistory, (Handler) => {
  render(<Handler/>, el);
})

// v1.0
import createBrowserHistory from 'history/lib/createBrowserHistory'
let history = createBrowserHistory()
render(<Router history={history}>{routes}</Router>, el)

如果没有指定一个 history 的类型(如上面的例子),那么你将到 1.0.0 后会发现一些不同寻常的行为。使用默认的基于哈希的路由查询字符串不由自己定义,将开始出现在你的网址被称为_k。例如会这样:?_k=umhx1s

这是 createHashHistroy(也就是,如果没有指定一个默认的 history 方法) 的一部分。

Route Config

你还是可以跟以前一样嵌套路由,路径还是跟以前一样继承自父母,但是支柱的名称改变了。

// v0.13.x
<Route name="about" handler={About}/>

// v1.0
<Route path="about" component={About}/>

NotFound route

// v0.13.x
<NotFoundRoute handler={NoMatch}/>

// v1.0
<Route path="*" component={NoMatch}/>

Redirect route

// v0.13.x
<Redirect from="some/where/:id" to="somewhere/else/:id" params={{id: 2}}/>

// v1.0
// 除了参数都放在路由上,其他都跟之前一样,
<Redirect from="/some/where/:id" to="/somewhere/else/2"/>

Links

path / params

如果学过 angular 的同学对这部分的改变会比较的熟悉,并会对此大赞!!

// v0.13.x
<Link to="user" params={{userId: user.id}}>Mateusz</Link>

// v1.0
// 链接到整个路径,再也不用知道参数的名称并且字符串模板很美观大方
<Link to={`/users/${user.id}`}>Mateusz</Link>
Linking to Index routes
// v0.13.x
// with this route config<Route path="/" handler={App}> <DefaultRoute name="home" handler={Home}/>
  <Route name="about" handler={About}/></Route>// will be active only when home is active, not when about is active<Link to="home">Home</Link>// v1.0<Route path="/" component={App}> <IndexRoute component={Home}/> <Route path="about" component={About}/></Route>// will be active only when home is active, not when about is active<IndexLink to="/">Home</IndexLink>
"active" class

在 0.13.x 版本,链接默认的添加active类,你可以覆盖activeClassName ,或提供activeStyles 。通常只是少数人需要的这种导航。

链接不再默认添加active类(因为成本较高并且通常不必要),你可选择设置一个。如果activeClassName或者activeStyles都没有设置,那么链接就不会自动检测它的是否 active。

// v0.13.x
<Link to="about">About</Link>

// v1.0
<Link to="/about" activeClassName="active">About</Link>

Linking to Index routes

// v0.13.x
// with this route config
<Route path="/" handler={App}>
  <DefaultRoute name="home" handler={Home}/>
  <Route name="about" handler={About}/>
</Route>

// will be active only when home is active, not when about is active
<Link to="home">Home</Link>

// v1.0
<Route path="/" component={App}>
  <IndexRoute component={Home}/>
  <Route path="about" component={About}/>
</Route>

// will be active only when home is active, not when about is active
<IndexLink to="/">Home</IndexLink>

RouteHandler

没有RouteHandler 了. Router在是动态的路由的基础上,自动在你组件的this.props.children里。

// v0.13.x
<RouteHandler/>
<RouteHandler someExtraProp={something}/>

// v1.0
{this.props.children}
{React.cloneElement(this.props.children, {someExtraProp: something})}

这种写法有一点语义上的改变。React 在当元素被创建时候验证propTypes,比当它们即将被渲染的时候好。
更多详情,看这里:
facebook/react#4494.

Navigation Mixin

If you were using the Navigation mixin, use the History mixin instead.
Navigation mixin,现在用History mixin替代。

// v0.13.x
var Assignment = React.createClass({
  mixins: [ Navigation ],
  navigateAfterSomethingHappened () {
    this.transitionTo('/users', { userId: user.id }, query);
    // this.replaceWith('/users', { userId: user.id }, query);
  }
})

// v1.0
var Assignment = React.createClass({
  mixins: [ History ],
  navigateAfterSomethingHappened () {
    // the router is now built on rackt/history, and it is a first class
    // API in the router for navigating
    this.history.pushState(null, `/users/${user.id}`, query);
    // this.history.replaceState(null, `/users/${user.id}`, query);
  }
})

之前Navigation下的方法,在 history 下也有,主要的不同在于没有参数或者路由名称,仅仅是路径名。

v0.13 v1.0
go(n) go(n)
goBack() goBack()
goForward() goForward()
makeHref(routeName, params, query) createHref(pathname, query)
makePath(routeName, params, query) createPath(pathname, query)

State mixin

// v0.13.x
var Assignment = React.createClass({
  mixins: [ State ],
  foo () {
    this.getPath()
    this.getParams()
    // etc...
  }
})

// v1.0
// if you are a route component...
<Route component={Assignment} />

var Assignment = React.createClass({
  foo () {
    this.props.location // contains path information
    this.props.params // contains params
    this.props.history.isActive('/pathToAssignment')
  }
})

// if you're not a route component, you need to pass location down the
// tree or get the location from context. We will probably provide a
// higher order component that will do this for you but haven't yet.
// see further down for more information on what can be passed down
// via context
var Assignment = React.createClass({
  contextTypes: {
    location: React.PropTypes.object
  },
  foo () {
    this.context.location
  }
})

下表展示了你过去在Statemixin下获取东西的方法和如果是组件的话,现在获取方法

v0.13 (this) v1.0 (this.props)
getPath() location.pathname+location.search
getPathname() location.pathname
getParams() params
getQuery() location.search
getQueryParams() location.query
getRoutes() routes
isActive(to, params, query) history.isActive(pathname, query, onlyActiveOnIndex)

下表是过去通过State获取属性方法,和现在如果不是组件的情况下获取的方法对比

v0.13 (this) v1.0 (this.context)
getPath() location.pathname+location.search
getPathname() location.pathname
getQuery() location.query
isActive(to, params, query) history.isActive(pathname, query, indexOnly)

注意:在 v1.0 不是所有的State功能都有。例如,params通过上下文是没用的。

本文译自:
https://github.com/rackt/react-router/blob/master/CHANGES.md

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容