zoukankan      html  css  js  c++  java
  • ReactRouter基本使用2

    十、histroy 属性

    Router组件的history属性,用来监听浏览器地址栏的变化,并将URL解析成一个地址对象,供 React Router 匹配。

    history属性,一共可以设置三种值。

    • browserHistory
    • hashHistory
    • createMemoryHistory

    如果设为hashHistory,路由将通过URL的hash部分(#)切换,URL的形式类似example.com/#/some/path

    import { hashHistory } from 'react-router'
    
    render(
      <Router history={hashHistory} routes={routes} />,
      document.getElementById('app')
    )

    如果设为browserHistory,浏览器的路由就不再通过Hash完成了,而显示正常的路径example.com/some/path,背后调用的是浏览器的History API。

    import { browserHistory } from 'react-router'
    
    render(
      <Router history={browserHistory} routes={routes} />,
      document.getElementById('app')
    )

    但是,这种情况需要对服务器改造。否则用户直接向服务器请求某个子路由,会显示网页找不到的404错误。

    如果开发服务器使用的是webpack-dev-server,加上--history-api-fallback参数就可以了。

    
    $ webpack-dev-server --inline --content-base . --history-api-fallback
    

    createMemoryHistory主要用于服务器渲染。它创建一个内存中的history对象,不与浏览器URL互动。

    
    const history = createMemoryHistory(location)

    十一、表单处理

    Link组件用于正常的用户点击跳转,但是有时还需要表单跳转、点击按钮跳转等操作。这些情况怎么跟React Router对接呢?

    下面是一个表单。

    
    <form onSubmit={this.handleSubmit}>
      <input type="text" placeholder="userName"/>
      <input type="text" placeholder="repo"/>
      <button type="submit">Go</button>
    </form>
    

    第一种方法是使用browserHistory.push

    import { browserHistory } from 'react-router'
    
    // ...
      handleSubmit(event) {
        event.preventDefault()
        const userName = event.target.elements[0].value
        const repo = event.target.elements[1].value
        const path = `/repos/${userName}/${repo}`
        browserHistory.push(path)
      },

    第二种方法是使用context对象。

    
    export default React.createClass({
    
      // ask for `router` from context
      contextTypes: {
        router: React.PropTypes.object
      },
    
      handleSubmit(event) {
        // ...
        this.context.router.push(path)
      },
    })

    十二、路由的钩子

    每个路由都有EnterLeave钩子,用户进入或离开该路由时触发。

    
    <Route path="about" component={About} />
    <Route path="inbox" component={Inbox}>
      <Redirect from="messages/:id" to="/messages/:id" />
    </Route>

    上面的代码中,如果用户离开/messages/:id,进入/about时,会依次触发以下的钩子。

    • /messages/:idonLeave
    • /inboxonLeave
    • /aboutonEnter

    面是一个例子,使用onEnter钩子替代<Redirect>组件。

    
    <Route path="inbox" component={Inbox}>
      <Route
        path="messages/:id"
        onEnter={
          ({params}, replace) => replace(`/messages/${params.id}`)
        } 
      />
    </Route>

    onEnter钩子还可以用来做认证。


    const requireAuth = (nextState, replace) => { if (!auth.isAdmin()) { // Redirect to Home page if not an Admin replace({ pathname: '/' }) } } export const AdminRoutes = () => { return ( <Route path="/admin" component={Admin} onEnter={requireAuth} /> ) }

    下面是一个高级应用,当用户离开一个路径的时候,跳出一个提示框,要求用户确认是否离开。

    const Home = withRouter(
      React.createClass({
        componentDidMount() {
          this.props.router.setRouteLeaveHook(
            this.props.route, 
            this.routerWillLeave
          )
        },
    
        routerWillLeave(nextLocation) {
          // 返回 false 会继续停留当前页面,
          // 否则,返回一个字符串,会显示给用户,让其自己决定
          if (!this.state.isSaved)
            return '确认要离开?';
        },
      })
    )

    上面代码中,setRouteLeaveHook方法为Leave钩子指定routerWillLeave函数。该方法如果返回false,将阻止路由的切换,否则就返回一个字符串,提示用户决定是否要切换。

  • 相关阅读:
    echarts更改鼠标悬浮时的文字
    echarts加定值横线
    js属性对象的hasOwnProperty方法
    React中使用回车键绑定事件
    使用html2canvas插件截图不全的问题,亲测有效!!!
    Windows用Putty以及Pageant配置SSH用于Github等身份验证
    andThenDemo 和Predicate的函数式
    延迟方法的调用---排列数字的大小(Supplier)
    延迟方法的调用---日志错误等级的输出(Supplier)
    延迟方法的调用---日志错误等级的输出
  • 原文地址:https://www.cnblogs.com/zhouyideboke/p/12530660.html
Copyright © 2011-2022 走看看