0.0.20 • Published 5 months ago

@hsu-react/single-router v0.0.20

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

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

0.0.20

5 months ago

0.0.20-alpha.1

5 months ago

0.0.19

6 months ago

0.0.19-alpha.5

6 months ago

0.0.19-alpha.4

6 months ago

0.0.19-alpha.3

6 months ago

0.0.19-alpha.2

6 months ago

0.0.19-alpha.1

6 months ago

0.0.18

7 months ago

0.0.18-alpha.1

7 months ago

0.0.17

7 months ago

0.0.17-alpha.6

7 months ago

0.0.17-alpha.5

7 months ago

0.0.17-alpha.4

7 months ago

0.0.17-alpha.3

7 months ago

0.0.17-alpha.2

7 months ago

0.0.17-alpha.1

7 months ago

0.0.16

7 months ago

0.0.15

7 months ago

0.0.14

7 months ago

0.0.13

7 months ago

0.0.12

7 months ago

0.0.12-alpha.3

7 months ago

0.0.12-alpha.2

7 months ago

0.0.12-alpha.1

7 months ago

0.0.11

7 months ago

0.0.11-alpha.25

7 months ago

0.0.11-alpha.24

7 months ago

0.0.11-alpha.23

7 months ago

0.0.11-alpha.22

7 months ago

0.0.11-alpha.21

7 months ago

0.0.11-alpha.20

7 months ago

0.0.11-alpha.19

7 months ago

0.0.11-alpha.18

7 months ago

0.0.11-alpha.17

7 months ago

0.0.11-alpha.16

7 months ago

0.0.11-alpha.15

7 months ago

0.0.11-alpha.14

7 months ago

0.0.11-alpha.13

7 months ago

0.0.11-alpha.12

7 months ago

0.0.11-alpha.11

7 months ago

0.0.11-alpha.10

7 months ago

0.0.11-alpha.9

7 months ago

0.0.11-alpha.8

7 months ago

0.0.11-alpha.7

7 months ago

0.0.11-alpha.5

7 months ago

0.0.11-alpha.4

7 months ago

0.0.11-alpha.3

7 months ago

0.0.11-alpha.2

7 months ago

0.0.11-alpha.1

7 months ago

0.0.10

7 months ago

0.0.10-alpha.13

7 months ago

0.0.10-alpha.12

7 months ago

0.0.10-alpha.11

7 months ago

0.0.10-alpha.10

7 months ago

0.0.10-alpha.9

7 months ago

0.0.10-alpha.8

7 months ago

0.0.10-alpha.7

7 months ago

0.0.10-alpha.6

7 months ago

0.0.10-alpha.5

7 months ago

0.0.10-alpha.4

7 months ago

0.0.10-alpha.3

7 months ago

0.0.10-alpha.2

7 months ago

0.0.10-alpha.1

7 months ago

0.0.9

8 months ago

0.0.9-alpha.2

8 months ago

0.0.9-alpha.1

8 months ago

0.0.8

10 months ago

0.0.7-author.1

10 months ago

0.0.7

10 months ago

0.0.6-alpha.2

10 months ago

0.0.6-alpha.1

10 months ago

0.0.6

10 months ago

0.0.5-alpha.6

10 months ago

0.0.5-alpha.5

10 months ago

0.0.5-alpha.4

10 months ago

0.0.5-alpha.3

10 months ago

0.0.5-alpha.2

10 months ago

0.0.5-alpha.1

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.2-alpha.48

10 months ago

0.0.2-alpha.47

10 months ago

0.0.2-alpha.46

10 months ago

0.0.2-alpha.44

10 months ago

0.0.2-alpha.43

10 months ago

0.0.2-alpha.42

10 months ago

0.0.2-alpha.41

10 months ago

0.0.2-alpha.40

10 months ago

0.0.2-alpha.39

10 months ago

0.0.2-alpha.38

10 months ago

0.0.2-alpha.37

10 months ago

0.0.2-alpha.36

10 months ago

0.0.2-alpha.35

10 months ago

0.0.2-alpha.34

10 months ago

0.0.2-alpha.33

10 months ago

0.0.2-alpha.32

10 months ago

0.0.2-alpha.31

10 months ago

0.0.2-alpha.30

10 months ago

0.0.2-alpha.29

10 months ago

0.0.2-alpha.28

10 months ago

0.0.2-alpha.27

10 months ago

0.0.2-alpha.26

10 months ago

0.0.2-alpha.25

10 months ago

0.0.2-alpha.24

10 months ago

0.0.2-alpha.23

10 months ago

0.0.2-alpha.22

10 months ago

0.0.2-alpha.21

10 months ago

0.0.2-alpha.20

10 months ago

0.0.2-alpha.19

10 months ago

0.0.2-alpha.18

10 months ago

0.0.2-alpha.17

10 months ago

0.0.2-alpha.16

10 months ago

0.0.2-alpha.15

10 months ago

0.0.2-alpha.14

10 months ago

0.0.2-alpha.13

10 months ago

0.0.2-alpha.12

10 months ago

0.0.2-alpha.11

10 months ago

0.0.2-alpha.10

10 months ago

0.0.2-alpha.9

10 months ago

0.0.2-alpha.8

10 months ago

0.0.2-alpha.7

10 months ago

0.0.2-alpha.6

10 months ago

0.0.2-alpha.5

10 months ago

0.0.2-alpha.4

10 months ago

0.0.2-alpha.3

10 months ago

0.0.2-alpha.2

10 months ago

0.0.2-alpha.1

10 months ago

0.0.1

10 months ago