zoukankan      html  css  js  c++  java
  • react-route简单实现

    调用界面

    import React, {Component} from 'react';
    // import {BrowserRouter, Link, Route, Switch} from "react-router-dom";
    import {BrowserRouter, Link, Route} from "../../myReactRoute"; // 引入自己定义的文件
    import UserPage from "./userPage";
    import Home from "./Home";
    class RouteCom extends Component {
        render() {
            return (
                <div>
                    <h1>routeCom</h1>
                    <BrowserRouter>
                            <Link to={'/'}>首页</Link>
                            <Link to={'/user'}>用户中心</Link>
                            <Link to={'/404'}>404</Link>
    
                            <Route exact path={'/'} component={Home}></Route>
                            <Route path={'/user'} component={UserPage}></Route>
                            <Route component={() => <h1>404</h1>} />
    
                    </BrowserRouter>
    
                </div>
            );
        }
    }
    
    export default RouteCom;

    自定义实现界面

    import React, {Component, useContext} from 'react';
    import {createBrowserHistory} from 'history' // 该包可以获取当前路径信息
    const RouteContext = React.createContext() // 创建context传值
    const RouteProvider = RouteContext.Provider
    const RouteConsumer = RouteContext.Consumer
    export class BrowserRouter extends Component {
        constructor(props) {
            super(props);
            this.history = createBrowserHistory()
            console.log(this.history)
            this.state = {
                location: this.history.location //将路径信息值传递给state以便进行双向绑定
            }
            this.unlisten = this.history.listen((location) => {
                this.setState({
                    location
                })
            })
            // console.log(this.history)
        }
        componentWillUnmount() {
            this.unlisten && this.unlisten() // 退出销毁监听
        }
    
        render() {
            const {children} = this.props
            const {location} = this.state
            return (
                // 将路径信息值传递
                <RouteProvider value={{history: this.history, location: location}}>
                    {children}
                </RouteProvider>
            );
        }
    }
    export  class Link extends Component { // link的本质还是一个a标签
        handleClick = (event, ctx) => {
            event.preventDefault() // 屏蔽掉默认的a标签的事件
            ctx.history.push(this.props.to) // 手动执行history事件
        }
        render() {
            const {children, to} = this.props
            return (
                <RouteConsumer>
                    {
                       (ctx) => (
                        <a href={to} onClick={event => this.handleClick(event, ctx)}>
                        {children}
                        </a>
                        )
                    }
                </RouteConsumer>
    
            );
        }
    }
    export  function Route (props){
    
        const ctx = useContext(RouteContext) // 因为是函数组件,所以调用useContext拿到传递的值
        const {location} = ctx
        const {component, path} = props // props是该组件接受到的传值参数
        const matchRoute = location.pathname === path // 页面路径和传递来的路径相同则显示对应组件
        return (
            matchRoute && component ? React.createElement(component) : null // 通过React.createElement可以将component渲染出来
        )
    }
  • 相关阅读:
    vue 富文本编译器 vue-quill-editor
    vue-拖拽的使用awe-dnd
    Dapper是什么?
    如何运用领域驱动设计
    面试官问我MySQL索引,我
    MySQL:你知道什么是覆盖索引吗?
    mysql覆盖索引与回表
    C#.NET 字符串转数组,数组转字符串
    MYSQL如何让主键使用BTREE索引
    MySQL大表优化方案 Mysql的row_format(fixed与dynamic)
  • 原文地址:https://www.cnblogs.com/cazj/p/14267311.html
Copyright © 2011-2022 走看看