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

@hsu-react/single-router

v0.0.20

Published

single-router

Downloads

16

Readme

Single Router

前言

single-router 可以在不改变浏览器路由的情况下,以类似路由跳转的方式变更页面

参考

安装

npm install --save @hsu-react/single-router
# 或
yarn add @hsu-react/single-router

组件

SingleRouter

import React from "react";
import App from "./App";
import { SingleRouter } from "@hsu-react/single-router";

const root = ReactDOM.createRoot(
  document.getElementById("root") as HTMLElement
);
root.render(
  <React.StrictMode>
    // showPath 默认为true 控制路由框是否展示 仅在开发环境作用
    <SingleRouter showPath={true}>
      <App />
    </SingleRouter>
  </React.StrictMode>
);

Route

基本使用

import React from "react";
import { Route } from "@hsu-react/single-router";

const App: React.FC = () => {
  return (
    <div className="App">
      // 可通过 useRoutes 生成
      <Route path="/path1" component={<AppOne />} />
      <Route path="/path2" component={<AppTwo />} />
      ...
    </div>
  );
};

export default App;

嵌套路由

import React from "react";
import { Route } from "@hsu-react/single-router";

const AppOne: React.FC = () => {
  return (
    <div className="AppOne">
      // 1. 需要完整路由
      // 2. 若想跳转 "/path1/path1-1" 需要先进入 <AppOne />
      // 3. 原地加载 <AppOneOne />
      // 4. 可通过 useRoutes 生成
      <Route path="/path1/path1-1" component={<AppOneOne />} />
      ...
    </div>
  );
};

export default AppOne;

HOOKS

useRoutes

useRoutes 可以根据路由树,生成路由

import { Routes } from "@hsu-react/single-router";

const ROUTERS: Routes = [
  {
    path: "/path1",
    component: <AppOne />,
  },
  {
    path: "/path2",
    component: <AppTwo />,
  },
  {
    path: "/path3",
    // children 中的路由平级, 且与 "/path1"、"/path2" 平级
    children: [
      {
        index: true, // 相当于 'path: "/path3"'
        component: <AppThree />,
      },
      {
        // path: "/path3/:id",
        // 或
        path: "/:id", // 会自动添加 "/path3",相当于 "/path3/:id"
        component: <AppThreeOne />,
      },
    ]
  },
  ...
];

export default ROUTERS;
import { useRoutes } from "@hsu-react/single-router";
import ROUTERS from "./Routers";
...

const App: React.FC = () => {
  const Routes = useRoutes(ROUTERS);

  return (
    <div className="App">
      {Routes}
    </div>
  );
};

useNavigate

通过 useNavigate 进行路由跳转

import React, { useEffect } from "react";
import { useNavigate } from "@hsu-react/single-router";
...

const App: React.FC = () => {
  ...

  const navigate = useNavigate();

  useEffect(() => {
    // 跳转
    // 跳转的路由会被记录在 history 中
    navigate("/path1");
    // 将history重置并跳转
    navigate("/path1", { replace: true });

    // 前进 | 后退
    // 若超出history的范围,则不会被执行
    // navigate(1)
    // navigate(-1)
    // 会将其后的路由都将从 history 中删除
    // navigate(1, { replace: true })
    // navigate(-1 , { replace: true })
  }, [navigate]);

  ...
};

useLocation

使用 useLocation 获取当前路由

import React, { useEffect } from "react";
import { useLocation } from "@hsu-react/single-router";
...

const App: React.FC = () => {
  ...

  const location = useLocation();

  useEffect(() => {
    // {pathname: string, history: string[], index: number}
    // 初始状态
    // {pathname: '', history: [], index: -1}
    console.log(location)
  }, [location]);

  ...
};

useParams

使用 useParams 获取动态路由参数

import React, { useEffect } from "react";
import { useParams } from "@hsu-react/single-router";
...

const App: React.FC = () => {
  ...

  // return {id: string | undefined}
  const { id } = useParams<{ id: string }>();

  useEffect(() => {
    console.log(id);
  }, [id]);

  ...
};

useSearch

使用 useSearch 获取查询参数参数

import React, { useEffect } from "react";
import { useSearch } from "@hsu-react/single-router";
...

const App: React.FC = () => {
  ...

  // return {id: string | undefined}
  const [{ id }, setSearch] = useSearch<{ id: string }>();

  useEffect(() => {
    console.log(id);

    // 修改当前路由search, 并新增一条记录
    setSearch({id: [1]})
    // 修改当前路由search, 历史路由记录不变
    setSearch({id: [1]}, { replace: true })
  }, [id]);

  ...
};

License

MIT