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

router-naive-guard

v0.1.0

Published

"a small router guard based on react-router v6 which can be used simply and easily"

Downloads

5

Readme

依赖

  1. react 版本 > 17
  2. react-router-dom v6版本可用

使用方法

  1. useGuardedRoutes创建
import { Navigate } from 'react-router-dom'
import Layout from '../pages/Layout'
import { lazy, Suspense } from 'react'
import GuardedRoute, { useGuardedRoutes, IGuardedRoute } from './GuardedRoute'

export enum RoutePath {
  A = '/a',
  B = '/b',
  C = '/c'
}

const lazyElement = (routePath: string) => {
  const Component = lazy(() => import(routePath))
  return (
    <Suspense fallback={<div> 加载中... </div>}>
      <Component />
    </Suspense>
  )
}

export const routes: IGuardedRoute[] = [
  {
    path: '/',
    element: <Navigate to={RoutePath.A} replace={true} />,
    isGuarded: false
  },
  {
    element: <Layout />,
    isGuarded: false,
    children: [
      {
        path: RoutePath.A,
        element: lazyElement('../pages/A'),
        guard: () => true,
        isGuarded: true
      },
      {
        path: RoutePath.B,
        element: lazyElement('../pages/B'),
        guard: () => true,
        isGuarded: true
      },
      {
        path: RoutePath.C,
        element: lazyElement('../pages/C'),
        guard: () => false,
        redirect: '/b',
        isGuarded: true
      }
    ]
  },
  {
    path: '*',
    element: lazyElement('../pages/Error'),
    isGuarded: false
  }
]

export default function Routes() {
  return useGuardedRoutes(routes)
}
  1. GuardedRoute创建
import { Navigate, useRoutes } from 'react-router-dom'
import Layout from '../pages/Layout'
import { lazy, Suspense } from 'react'
import GuardedRoute, { useGuardedRoutes, IGuardedRoute } from './GuardedRoute'

export enum RoutePath {
  A = '/a',
  B = '/b',
  C = '/c'
}

const lazyElement = (routePath: string, ...rest) => {
  const Component = lazy(() => import(routePath))
  return (
    <GuardedRoute 
    guard={rest.guard} 
    redirect={rest.redirect} 
    replace={rest.replace} 
    state={rest.replace}>
      <Suspense fallback={<div> 加载中... </div>}>
        <Component />
      </Suspense>
    </GuardedRoute>
  )
}

export const routes: IGuardedRoute[] = [
  {
    path: '/',
    element: <Navigate to={RoutePath.A} replace={true} />
  },
  {
    element: <Layout />,
    children: [
      {
        path: RoutePath.A,
        element: lazyElement('../pages/A')
      },
      {
        path: RoutePath.B,
        element: lazyElement('../pages/B')
      },
      {
        path: RoutePath.C,
        element: lazyElement('../pages/C')
      }
    ]
  },
  {
    path: '*',
    element: lazyElement('../pages/Error'),
  }
]

export default function Routes() {
  return useRoutes(routes)
}

具体参数

| GuardedRoute | 描述 | | ------------ | --------------------------------------- | | guard?: () => boolean | 路由守卫,返回boolean值以确定是否渲染 | | redirect?: string | 提供重定向地址,默认/ | | replace?: boolean | 开启replace,默认false | | state?: Record<string, any> | 重定向跳转携带参数,会添加from: '跳转而来的路由地址' | | isGuarded?: boolean | 是否开启守卫,只有在使用useGuardedRoutes创建时有用 |