Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/away0x/react-router-helper

react router helper
https://github.com/away0x/react-router-helper

middleware react react-router react-router-config typescript

Last synced: 16 days ago
JSON representation

react router helper

Awesome Lists containing this project

README

        

# React Router Helper
> 类似 react-router-config,可以以配置形式生成路由

## Install
```bash
npm install --save aw-react-router-helper
```

## Types
```typescript
type RoutesRenderFun = () => React.ReactNode;

interface RouteState {
path: string;
meta: Meta;
url: string;
}

interface RouteConfig {
// 路径
path: string;
// 存储一些路由的额外信息
meta?: Meta;
// 重定向 route path
redirect?: string;
// 路由的展示组件 (二选一,且必须有其一)
component?: React.ComponentType> | React.ComponentType;
render?: (props: RouteConfigComponentProps) => React.ReactNode;
// 中间件 (路由渲染的拦截器)
middlewares?: RouteMiddlewareFunc[];
// 子路由
routes?: RouteConfig[];
}

interface RouteConfigComponentProps extends RouteComponentProps {
// 路由配置信息
route: RouteConfig;
// 渲染子路由的方法
renderRoutes: RoutesRenderFun;
}

type RouteMiddlewareFunc = (params: RouteState) => undefined | JSX.Element | string;

type RouteLoadOptions = {
configs: RouteConfig[];
middlewares?: RouteMiddlewareFunc[];
}

interface IRouterHelper {
load(options: RouteLoadOptions): IRouterHelper;
render(routes?: RouteConfig[]): JSX.Element | null;
}
```

## Examples
- [base](examples/src/base.tsx)
- [auth](examples/src/auth.tsx)

***