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-block

v0.1.5

Published

基于react-router v6版本的路由守卫,让你能使用类似vue-router那样的路由守卫

Downloads

8

Readme

bundle-size npm-version license language

About

react-router-block是一个基于react-router v6版本的路由守卫,让你能使用像类似vue-router那样的路由守卫,虽然该守卫也能用于页面级的组件,但建议用在App.tsx里面当作全局的路由守卫即可,建议您在页面级的组件应该使用hooks来进行判断与跳转

Install


 npm install react-router-block -S

types


 interface To {
     path: string;
     search:Search;
     state:unknow;
     key:Key;
     hash:Hash
 }
type From = To | null;
type Next = (path?: string) => void;
interface RouterBlockProps {
    routes?: RouteObject[];
    beforeEnter: (to: To, next: Next, from: From) => void;
    children?: React.ReactNode
}

Usage

RouterBlock可以传入两个参数

  1. 第一个参数是beforeEnter, 调用next可以函数跳转到你要去的页面,调用多次以最后一次为准 (必传)

  2. 一个是routes,就是react-router使用useRoutes配置的路由表,配置项和react-router的一模一样(可选) 或者一个children(在RouterBlock组件内部配置路由,和react-router配置路由的方式一样),routes和children二者必须要有一个,如果两者都有,则children的配置会生效,使用children的方式配置路由的时候可以使用懒加载


import {Routes,Route} from 'react-router-dom'
// 导入类型和组件
import  RouterBlock  from 'react-router-block'
// 导入路由表
import routes from './router';

// 懒加载导入,使用children的写法可以使用懒加载
const Home = lazy(() =>import ("./pages/Home"))
const Cart = lazy(() =>import ("./pages/Cart"))
const Login = lazy(() =>import ("./pages/Login"))
const Profile = lazy(() =>import ("./pages/Profile"))
const ProfileSetting = lazy(() =>import ("./pages/Profile/ProfileSetting"))
const ProfileDetail = lazy(() =>import ("./pages/Profile/ProfileDetail"))
function App() {
  const [msg,setMsg] =useState('未登录')
  const [isLogin,setIsLogin] = useState(false)
  return (
    <div className="App">
      {/*routes的写法*/}
      <RouterBlock routes={routes} beforeEnter={(to,next,from) => {
        if (to.path!=='/login' && to.path!=="/" && !isLogin) {
          next('/login');
        }else {
          next()
        }
      }}>
        {/* 使用children的写法 */}
         <Routes>
           <Route path="/" element={<Suspense><Home/></Suspense>} />
           <Route path="/cart" element={<Suspense><Cart/></Suspense>} />
           <Route path="/login" element={<Suspense><Login/></Suspense>} />
           <Route path="/profile" element={<Suspense><Profile/></Suspense>} >
              <Route index element={<Suspense><ProfileSetting/></Suspense>} />
              <Route path="setting" element={<Suspense><ProfileSetting/></Suspense>} />
              <Route path="detail" element={<Suspense><ProfileDetail/></Suspense>} />
           </Route>
         </Routes>
      </RouterBlock>
    </div>
  ); 

  function handleClick() {
    if (!isLogin) {
      setMsg('已登录')
    }else {
      setMsg('未登录')
    }
    setIsLogin(!isLogin);
  }
}

export default App;

这儿有一个例子,建议你看一看

LICENSE

MIT