Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: intro routes prop config keepQuery #12228

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion docs/docs/docs/guides/routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,17 @@ export default {
}
```

访问 `/` 会跳转到 `/list`,并由 `src/pages/list` 文件进行渲染。
访问 `/` 会跳转到 `/list` 。

重定向时,默认不会携带原 url 的查询参数,如需保持原参数,添加 `keepQuery` 选项即可:

```ts
routes: [
{ path: '/', redirect: '/list', keepQuery: true },

// 注:若你需在跳转时处理参数,可以自行实现一个跳转组件
]
```

### wrappers

fz6m marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
21 changes: 17 additions & 4 deletions packages/renderer-react/src/routes.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
// @ts-ignore
import React, { useMemo } from 'react';
import { generatePath, Navigate, useParams, Outlet } from 'react-router-dom';
import React from 'react';
import {
generatePath,
Navigate,
Outlet,
useLocation,
useParams,
} from 'react-router-dom';
import { useAppData, useRouteProps } from './appContext';
import { RouteContext, useRouteData } from './routeContext';
import { IClientRoute, IRoute, IRoutesById } from './types';
import { useAppData } from './appContext';

export function createClientRoutes(opts: {
routesById: IRoutesById;
Expand Down Expand Up @@ -48,9 +54,16 @@ export function createClientRoutes(opts: {

function NavigateWithParams(props: { to: string }) {
const params = useParams();
let to = generatePath(props.to, params);
const routeProps = useRouteProps();
const location = useLocation();
if (routeProps?.keepQuery) {
const queryAndHash = location.search + location.hash;
to += queryAndHash;
}
const propsWithParams = {
...props,
to: generatePath(props.to, params),
to,
};
return <Navigate replace={true} {...propsWithParams} />;
}
Expand Down
Loading