zoukankan      html  css  js  c++  java
  • react-router

    基本的构建

    import ReactRouter from 'react-router';
    let {Route, Router, Link, IndexRoute} = ReactRouter.Route;
    .....
    
    let Nav = React.createClass({
      render () {
        return (
          <div>
            <ul>
              <li><Link to="/">Home</Link></li>
              <li><Link to="/about">About</Link></li>
              <li><Link to="/inbox">Inbox</Link></li>
            </ul>
          </div>
        )
      }
    });
    
    let App = React.createClass({
      render () {
        return (
          <div>
             <h1>App</h1>
             <Nav />
             {this.props.children || || 'welcome to app'}
          </div>
        )
      }
    });
    
    
    React.render((
      <Router>
        <Route path='/' component={App}>
          <IndexRoute component={Home}/>
          <Route path='about' component={About}/>
          <Route path='inbox' component={Inbox}/>
        </Route>
      </Router>
    ), document.body);
    

    嵌套组件结构

    let Inbox = React.createClass({
      render () {
        return (
          <div>
           <h3>inbox</h3>
           {this.props.children || "Welcome to your Inbox"}
          </div>
        )
      }
    });
    
    let Message = React.createClass({
      render() {
        var id = this.props.params.id;
        var datas = ['zero','one','two','three','four','five'];
        var data = datas[id] || 'none';
        var links = datas.map(function (data, index){
          return (<li key={index}><Link to={`/messages/{index}`} >{index}</Link></li>)
        })
        return (
          <div>
          {links}
          <h3>{data}</h3>
          </div>
        )
      }
    })
    
    React.render((
      <Router>
        <Route path="/" component={App}>
          .....
          <Route path="inbox" component={Inbox}>
            <Route path="messages/:id" component={Message} />
          </Route>
        </Route>
      </Router>
    ), document.body)
    

    整体组件的结构

    URL Components
    / App-> Home
    /about App -> About
    /inbox App -> Inbox
    /inbox/messages/:id App -> Inbox -> Message

    URL和结构

    • <link/>结构: <Link to="/inbox/messages/:id">Message</Link>

    • 如果想在url上解耦,结构上仍然成为子组件,改成

    <Route path="inbox" component={Inbox}>
        <Route path="/messages/:id" component={Message} />
     </Route>
    //
    <Link to="/messages/:id">Message</Link>
    
    • 解耦后想把/inbox/messages/:id的连接跳转到/messages/:id;
    <Route path="inbox" component={Inbox}>
       <Route path="/messages/:id" component={Message} />
       <Redirect from="messages/:id" to="/messages/:id" />
    </Route>
    

    钩子

    • onEnter, onLeave
    <Route path='about' component={About} onEnter={redirectToChild}/>
    
    //
    function redirectToChild(nextState, redirectTo, callBack) { 
      redirectTo('/about', '', {nextPathname: nextState.location.pathname});
    }
    
    • 钩子函数结构
    //EnterHook
    type EnterHook = (nextState: RouterState, replaceState: RedirectFunction, callback?: Function) => any;`
    
    type RouterState = {
      location: Location;
      routes: Array<Route>;
      params: Params;
      components: Array<Component>;
    };
    
    type RedirectFunction = (pathname: Pathname | Path, query: ?Query, state: ?LocationState) => void;
    
    //LeaveHook
    type LeaveHook = () => any;
    

    路径匹配

    <Route path="/hello/:name">         // matches /hello/michael and /hello/ryan
    <Route path="/hello(/:name)">       // matches /hello, /hello/michael, and /hello/ryan
    <Route path="/files/*.*">           // matches /files/hello.jpg and /files/path/to/hello.jpg
    

    与上一版的具体变化

    传入组件的参数

    • 在配置好路径后, <Route>组件会将一下参数传入this.props

    history: Object

    • 常用history.pushState():来改变当前路径;

    location: Object

    • 当下路径解析后的参数对象;

    params: Object

    • 当前路径的param值;
    route: Object

    routeParams: Object

    routes: Array

  • 相关阅读:
    python 计算 父亲节
    python 计算 母亲节
    python 计算 感恩节
    《AINLP年度阅读收藏清单》,2020-01-01,52nlp
    《命名实体识别 NER 论文综述:那些年,我们一起追过的却仍未知道的花名 (一)》,2020-05,龚俊民(昵称: 除夕)
    《How to Automate Manual Steps after SSH》2019-03,amitness
    《Back Translation for Text Augmentation with Google Sheets》,2020-02,amitness
    《BERT 的优秀变体:ALBERT 论文图解介绍》2020-05,作者:amitness,译者:ronghuaiyang
    《NLP中数据增强的综述,快速的生成大量的训练数据》2020-05,作者:amitness ,编译:ronghuaiyang
    《努力成为优秀的工程师》李航,2013-03
  • 原文地址:https://www.cnblogs.com/jinkspeng/p/4803666.html
Copyright © 2011-2022 走看看