npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-router-waiter

v1.1.7

Published

react-router v6 路由统一管理及路由拦截方案

Downloads

25

Readme

react-router-waiter

react-router v6 路由统一管理 及 路由拦截方案。

1、安装

npm i react-router-waiter -S

2、使用

// 在项目入口文件index.js或入口组件App.js里引入
import { HashRouter } from 'react-router-dom' // 引入官方路由组件
import RouterWaiter from 'react-router-waiter' // 引入该插件
import routes from './routes' // 引入你的路由配置
import onRouteBefore from './onRouteBefore' // 引入你定义的路由拦截函数

function App () {
  return (
    <HashRouter>
      <RouterWaiter routes={routes} onRouteBefore={onRouteBefore} />
    </HashRouter>
  )
}

export default App

3、配置路由列表

const routes = [
  {
    path: '/',
    redirect: '/index',
  },
  {
    path: '/index',
    component: () => import(/* webpackChunkName: "index" */ '@/views/index/index'),
    meta: {
      title: '首页',
      needLogin: true,
    },
  },
]

export default routes
  • component 属性需使用 import()懒加载方式引入
  • 通过react-router官方的element字段也能配置路由组件,但element配置的不支持路由拦截。
  • 嵌套路由的使用请看下面的注意事项

4、配置路由拦截函数

/**
 * @param {string} pathname 当前路由路径
 * @param {object} meta 当前路由自定义meta字段
 * @return {string} 需要跳转到其他页时,就返回一个该页的path路径,或返回resolve该路径的promise对象
 */
const onRouteBefore = ({ pathname, meta }) => {
  // 示例:动态修改页面title
  if (meta.title !== undefined) {
    document.title = meta.title
  }
  // 示例:判断未登录跳转登录页
  if (meta.needLogin) {
    if (!isLogin) {
      return '/login'
    }
  }
}

export default onRouteBefore

5、API

主组件 RouterWaiter 的配置属性 API:

  • routes,[Array] 路由配置列表(必填)
  • onRouteBefore,[Function] 路由拦截函数(可选)
  • loading,[Element] 懒加载路由切换时的 loading 效果组件(可选)

路由配置列表 routes 的配置项 API:

  • redirect,[String] 要重定向的路由路径
  • component,[Function] import()懒加载方式引入的组件
  • meta,[Object] 自定义的数据

(优先级:redirect > component > element)

6、注意事项

  • react-router 的嵌套路由父级使用懒加载方式引用公共组件时存在一些问题,例如切换子路由时父级公共组件会重新渲染。建议改用官方element属性方式:
import PageLayout from '@/components/PageLayout' // 静态引入,不要使用import函数

{
  path: '/',
  element: <PageLayout />, // 父级的公共组件使用element配置
  children: [
    ... // 子级可以继续使用component配置
  ]
},
  • 这里的路由拦截是目标路由加载前的拦截,且是运行在顶层环境(top level),非react组件环境。所以在写路由拦截函数时,不能使用react组件hook(如useState),也应尽量避免引起目标路由重新渲染。

7、TS类型

import {
  RoutesType, // 路由配置数组类型
  RoutesItemType, // 路由配属数组项类型
  OnRouteBeforeType, // 路由拦截函数类型
  RouterWaiterPropsType, // RouterWaiter主组件props类型
} from 'react-router-waiter'

8、应用

react-antd-mobx-admin:react后台管理系统 - 项目模板