React-router 简单使用

2016-03-03  本文已影响363人  Moon_Yue

Route Configuration

路由配置就是一组基本指令,告诉路由如何尝试匹配URL和代码运行时做什么。下面是一个最基本的简单配置

import React from 'react'
import { render } from 'react-dom'
import { Router, Route, Link } from 'react-router'

const App = React.createClass({
  render() {
    return (
      <div>
        <h1>App</h1>
        <ul>
          <li><Link to="/about">About</Link></li>
          <li><Link to="/inbox">Inbox</Link></li>
        </ul>
        {this.props.children}
      </div>
    )
  }
})

const About = React.createClass({
  render() {
    return <h3>About</h3>
  }
})

const Inbox = React.createClass({
  render() {
    return (
      <div>
        <h2>Inbox</h2>
        {this.props.children || "Welcome to your Inbox"}
      </div>
    )
  }
})

const Message = React.createClass({
  render() {
    return <h3>Message {this.props.params.id}</h3>
  }
})

render((
  <Router>
    <Route path="/" component={App}>
      <Route path="about" component={About} />
      <Route path="inbox" component={Inbox}>
        <Route path="messages/:id" component={Message} />
      </Route>
    </Route>
  </Router>
), document.body)

这个示例知道如下呈现url

URL Components
/ App
/about App -> About
/inbox App -> Inbox
/inbox/messages/:id App ->Inbox ->Message
上一篇下一篇

猜你喜欢

热点阅读