react-router 学习笔记(1)

import React from "react";
import { BrowserRouter as Router, Route, Link } from "react-router-dom";

const BasicExample = () => (
  <Router>
    <div>
      <ul>
        <li>
          <Link to="/">Home</Link>
        </li>
        <li>
          <Link to="/about">About</Link>
        </li>
        <li>
          <Link to="/topics">Topics</Link>
        </li>
      </ul>
      <hr />

      <Route exact path="/" component={Home} />
      <Route path="/about" component={About} />
      <Route path="/topics" component={Topics} />
    </div>
  </Router>
);

const Home = () => (
  <div>
    <h2>Home</h2>
  </div>
);

const About = () => (
  <div>
    <h2>About</h2>
  </div>
);

const Topics = ({ match }) => (
  <div>
    <h2>Topics</h2>
    <ul>
      <li>
        <Link to={`${match.url}/rendering`}>Rendering with React</Link>
      </li>
      <li>
        <Link to={`${match.url}/components`}>Components</Link>
      </li>
      <li>
        <Link to={`${match.url}/props-v-state`}>Props v. State</Link>
      </li>
    </ul>

    <Route path={`${match.url}/:topicId`} component={Topic} />
    <Route
      exact
      path={match.url}
      render={() => <h3>Please select a topic.</h3>}
    />
  </div>
);

const Topic = ({ match }) => (
  <div>
    <h3>{match.params.topicId}</h3>
  </div>
);

export default BasicExample;

  知识点:

一、BrowserRouter

基于html5的history api提供url与ui界面的对应关系。

相关属性:

BaseName:基础名称,所有的Link和Redirect to的path前面都会增加Basename:示例

<BrowserRouter basename="/calendar"/>
<Link to="/today"/> // renders <a href="/calendar/today">

  

二、Link

主要作用是修改url

相当于《a》标签的封装,只是不自动向服务器请求并更新整个页面。

主要属性:

to:可以是一个字符串,也可以是一个对象,代表连接的地址。示例

<Link to="/about">About</Link>
<Link to='/courses?sort=name'/>
<Link to={{
  pathname: '/courses',
  search: '?sort=name',
  hash: '#the-hash',
  state: { fromDashboard: true }
}}/>

  

三、Route

可以说是React-router中最主要的组件,用来渲染与path相匹配的component/render/children

主要属性:

path:匹配的url,

<Route path="/users/:id" component={User}/>

<Route exact path="/one" component={About}/>

component:渲染的组件,将要接受Route props,包括:match,location和history;

  match:

    属性包含:

      params(从动态url中解析出的键值对

      path:路径模式,用于构建嵌套的《Route》

      url:url中匹配的部分,用于构建嵌套的Links.

  location:  包含key,pathname,search,hash,state. 

{
  key: 'ac3df4', // not with HashHistory!
  pathname: '/somewhere'
  search: '?some=search-string',
  hash: '#howdy',
  state: {
    [userDefined]: true
  }

  history:    

  • length - (number)历史堆栈中的条目数
  • action- (字符串)当前动作(PUSHREPLACE,或POP
  • location - (对象)当前位置。可能具有以下属性:
    • pathname - (字符串)URL的路径
    • search - (字符串)URL查询字符串
    • hash - (字符串)URL哈希片段
    • state- (对象)特定于位置的状态,例如push(path, state)当该位置被推入堆栈时。仅适用于浏览器和内存历史记录。
  • push(path, [state]) - (function)将新条目推送到历史堆栈
  • replace(path, [state]) - (function)替换历史堆栈中的当前条目
  • go(n)- (function)按n条目移动历史堆栈中的指针
  • goBack() - (功能)相当于 go(-1)
  • goForward() - (功能)相当于 go(1)
  • block(prompt)- (功能)防止导航

render:行内渲染

exact:精确匹配path

 

posted @ 2018-08-23 20:58  tutu_python  阅读(122)  评论(0)    收藏  举报