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-configurable-router

v1.1.0

Published

仿 vue-router 的 react 配置式路由,基于 react-router6

Downloads

12

Readme

react-configurable-router

仿 vue-router 的 react 配置式路由,基于 react-router6,额外支持了路由元信息、路由懒加载等功能

使用

添加路由组件

使用 export default 默认导出路由组件

// home/index.tsx
export default function Home() {
  return <div>Home</div>
}

路由配置文件

// routes.ts
import { type Route } from 'react-configurable-router'

const routes: Route[] = [
  {
    path: '/',
    lazyComponent: import('@/pages/home'),
    meta: {
      data: 'any',
    },
  },
  {
    path: '/user',
    children: [
      {
        path: 'login',
        lazyComponent: import('@/pages/user/login'),
      },
    ],
  },
  {
    path: '/redirect',
    redirect: '/user/login',
  },
  {
    path: '*',
    lazyComponent: import('@/pages/not-found'),
  }
]

配置 react-router-dom

import { ConfigurableRouter } from 'react-configurable-router'
import { BrowserRouter } from 'react-router-dom'
import { createRoot } from 'react-dom/client'

const root = createRoot(document.getElementById('root'))

// **使用传统路由**
root.render(
  <BrowserRouter>
    <ConfigurableRouter routes={routes}></ConfigurableRouter>
  </BrowserRouter>,
)

获取 meta 数据

有以下方式获取 meta 数据

1. 从 props 中获取 meta 元信息

import { type PropsWithMeta } from 'react-configurable-router'

export default function (props: PropsWithMeta) {
  const { meta } = props
}

2. 使用 useMetas 获取 meta 元信息

import { useMetas } from 'react-configurable-router'

export function Component() {
  const { metas } = useMetas()
}

API

// ConfigurableRouter

interface RouterProps {
  /**
   * 路由配置
   */
  routes: Route[]
  /**
   * 路由挂载之前执行,可用于拦截路由重定向
   */
  onRouteWillMount?: OnRouteWillMount
  /**
   * 路由挂载时执行
   */
  onRouteMount?: OnRouteMount
  /**
   * 路由卸载时执行
   */
  onRouteUnmount?: OnRouteUnmount
  /**
   * 增强渲染函数,用于自定义渲染逻辑
   * 可以跟动画库结合,实现路由切换动画
   */
  render?: (children: ReactNode | null) => ReactNode
  /**
   * 路由懒加载时的loading组件
   */
  suspense?: ReactNode
  /**
   * basename
   */
  basename?: string
}

相关