React.js实战之Router原理及 React-router

简介: 官网文档https://reacttraining.com/react-router/core/guides/philosophy页面路由Hash 路由H5路由只对后退记录有效// 页面路由window.

官网文档
https://reacttraining.com/react-router/core/guides/philosophy

img_785f519cb0211f99d072c62f471abe7c.png

img_ff7c397222695deb1605ea1f765996bf.png

页面路由

img_22dd6e091b8cb62a839f286f2e536ed2.png

Hash 路由

img_b561ff5c699eaefda9c870f3701ab100.png

img_4c17a25c7340780e46280808d73da5b1.png

H5路由

只对后退记录有效


img_522b17be923687c42c3c4352627bef1b.png

img_a461778a9b678d81da638d42f21c6558.png
// 页面路由
window.location.href = 'http://www.baidu.com';
history.back();

// hash 路由
window.location = '#hash';
window.onhashchange = function(){
    console.log('current hash:', window.location.hash);
}

// h5 路由
// 推进一个状态
history.pushState('name', 'title', '/path');
// 替换一个状态
history.replaceState('name', 'title', '/path');
// popstate
window.onpopstate = function(){
    console.log(window.location.href);
    console.log(window.location.pathname);
    console.log(window.location.hash);
    console.log(window.location.search);
}
img_373f138072e5a28822383097a5f483ac.png
// react-router
import React from 'react';
import ReactDOM from 'react-dom';
import { HashRouter as Router, Switch, Route, Link } from 'react-router-dom'


class A extends React.Component{
    constructor(props){
        super(props)
    }
    render(){
        return (
            <div>
                Component A

                <Switch>
                    <Route exact path={`${this.props.match.path}`} render={(route) => {
                        return <div>当前组件是不带参数的A</div>
                    }}/>
                    <Route path={`${this.props.match.path}/sub`} render={(route) => {
                        return <div>当前组件是Sub</div>
                    }}/>
                    <Route path={`${this.props.match.path}/:id`} render={(route) => {
                        return <div>当前组件是带参数的A, 参数是:{route.match.params.id}</div>
                    }}/>
                </Switch>
            </div>
        )
    }
}

class B extends React.Component{
    constructor(props){
        super(props)
    }
    render(){
        return <div>Component B</div>
    }
}

class Wrapper extends React.Component{
    constructor(props){
        super(props)
    }
    render(){
        return (
            <div>
                <Link to="/a">组件A</Link>
                <br/>
                <Link to="/a/123">带参数的组件A</Link>
                <br/>
                <Link to="/b">组件B</Link>
                <br/>
                <Link to="/a/sub">/a/sub</Link>
                {this.props.children}
            </div>
        );
    }
}

ReactDOM.render(
    <Router>
        <Wrapper>
            <Route path="/a" component={A}/>
            <Route path="/b" component={B}/>
        </Wrapper>
    </Router>,
    document.getElementById('app')
);

通过以上代码,首先演示 Hash 路由


img_3b005ed0bf852867da2fc04295d472d7.png

再演示 H5路由,即修改此处


img_174c6101667b55f8cf6e733bc396887b.png

img_726c67dda1963c43b8603605c9a87032.png

将参数传给组件
img_e66d8447ce9134d21000427ea3c12e5d.png
目录
相关文章
|
6月前
|
编解码 前端开发 JavaScript
js react antd 实现页面低分变率和高分变率下字体大小自适用,主要是配置antd
在React中结合Ant Design与媒体查询,通过less变量和响应式断点动态调整`@font-size-base`,实现多分辨率下字体自适应,提升跨设备体验。
297 2
|
前端开发 JavaScript
除了使用Route组件,React Router还有其他方式处理404错误页面吗
除了使用Route组件,React Router还有其他方式处理404错误页面吗
389 58
|
前端开发
如何在React Router中定义404错误页面组件?
如何在React Router中定义404错误页面组件?
355 57
|
前端开发 UED
在React Router中,如何处理路由的404错误页面?
在React Router中,如何处理路由的404错误页面?
397 57
|
前端开发
如何在React Router中进行嵌套路由配置?
如何在React Router中进行嵌套路由配置?
600 57
|
前端开发
如何在React Router中进行路由重定向?
如何在React Router中进行路由重定向?
723 57
|
前端开发 网络架构
如何在React Router中传递参数?
如何在React Router中传递参数?
443 57
|
移动开发 前端开发 API
React Router的用法。
React Router的用法。
243 56
|
11月前
|
前端开发 JavaScript NoSQL
使用 Node.js、Express 和 React 构建强大的 API
本文详细介绍如何使用 Node.js、Express 和 React 构建强大且动态的 API。从开发环境搭建到集成 React 前端,再到利用 APIPost 高效测试 API,适合各水平开发者。内容涵盖 Node.js 运行时、Express 框架与 React 库的基础知识及协同工作方式,还涉及数据库连接和前后端数据交互。通过实际代码示例,助你快速上手并优化应用性能。
|
前端开发 JavaScript 网络架构
实现动态路由与状态管理的SPA——使用React Router与Redux
【10月更文挑战第1天】实现动态路由与状态管理的SPA——使用React Router与Redux
451 95