React Router and Lifecycle

源码查看

使用终端

  • git clone git@github.com:boloog/react-router.git
  • npm i
  • npm run build
  • npm start

url地址

  • http://localhost:3000/#/ ( Home )
  • http://localhost:3000/#/article?orderBy=date ( Article )
  • http://localhost:3000/#/shows/1( Show )

组件的生命周期

  • componentDidMount ( 组件挂载 )
  • componentWillMount( 运行 render() )
  • componentWillReceiveProps( 读取 props )
  • shouldComponentUpdate( return true; 是否更新组件 )
  • componentWillUpdate( 更新组件 )
  • componentDidUpdate( 更新组件完毕 )
  • componentWillUnmount( 切换路由时组件将卸载 )

路由

import { Router, Route, hashHistory, Link, IndexRoute, Redirect } from 'react-router'

  • Router ( 一个容器 )
  • Route( 定义路由 )
  • hashHistory( 路由的切换由URL的hash变化决定 )
  • Link( 生成一个链接,允许用户点击后跳转到另一个路由 )
  • IndexRoute( 索引下对应显示内容 )
  • Redirect ( 路由重定向 )

相关代码

<Router history={hashHistory}>
    <Route path="/" component={App}>
        <IndexRoute component={Home} />
        <Route path="article" component={Article}>
            <IndexRoute component={ShowIndex} />
            <Route path="/shows/:id" component={Show}
                onEnter={handleEnter}
                onLeave={handleLeave}
            />
            <Redirect from="shows/:id" to="/shows/:id"/>
        </Route>
    </Route>
</Router>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容