Skip to content

Commit

Permalink
feat: 删除相对路径&升级 react-router 包版本
Browse files Browse the repository at this point in the history
  • Loading branch information
bigbigDreamer committed Oct 18, 2021
1 parent 68538ba commit bcf760f
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 60 deletions.
24 changes: 7 additions & 17 deletions packages/launcher/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/launcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
"react-hot-loader": "^4.13.0",
"react-loadable": "^5.5.0",
"react-redux": "^7.2.4",
"react-router": "^6.0.0-beta.0",
"react-router-dom": "^6.0.0-beta.0",
"react-router": "^6.0.0-beta.7",
"react-router-dom": "^6.0.0-beta.7",
"redux": "^4.1.0",
"redux-thunk": "^2.3.0"
},
Expand Down
11 changes: 2 additions & 9 deletions packages/launcher/src/hooks/use-router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useCallback, useMemo } from 'react';
import { useNavigate } from 'react-router-dom';
import { globalRouterBasePath } from '../utils/globalEnv';

export type UseRouterReturns = {
redirect(path: string, state?: UseRouterState): void;
Expand All @@ -14,10 +13,7 @@ function useRouter(): UseRouterReturns {

const pushPath = useCallback(
(path: string, state?: UseRouterState) => {
const basePath = globalRouterBasePath.get();
const resultPath = path.startsWith('/') ? basePath + path : path;

return navigate(resultPath, {
return navigate(path, {
state,
});
},
Expand All @@ -26,10 +22,7 @@ function useRouter(): UseRouterReturns {

const replacePath = useCallback(
(path: string, state?: UseRouterState) => {
const basePath = globalRouterBasePath.get();
const resultPath = path.startsWith('/') ? basePath + path : path;

return navigate(resultPath, {
return navigate(path, {
replace: true,
state,
});
Expand Down
5 changes: 1 addition & 4 deletions packages/launcher/src/router/render-routers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Route, Navigate } from 'react-router-dom';
import loadable from 'react-loadable';
import loading from './loading';
import WrapperRoute from './wrapper-route';
import { globalRouterBasePath } from '../utils/globalEnv';
import type { RouteItem, DynamicImportType } from '../app';
import type { ComponentType } from 'react';
import type { RouteProps } from 'react-router';
Expand All @@ -23,9 +22,7 @@ const renderRoutes = (

// RedirectRouteItem
if (!component && redirect) {
const basePath = globalRouterBasePath.get();
const redirectPath = redirect.startsWith('/') ? basePath + redirect : redirect;
const C = <Navigate to={redirectPath} />;
const C = <Navigate to={redirect} />;

// eslint-disable-next-line @typescript-eslint/no-use-before-define
return wrapperRoute({ path, element: C }, pluginRender);
Expand Down
6 changes: 1 addition & 5 deletions packages/launcher/src/router/wrapper-route.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useEffect } from 'react';
import { useLocation, useResolvedPath, useNavigate } from 'react-router-dom';
import { globalRouterBasePath } from '../utils/globalEnv';
interface WrapperRouteProps {
children: JSX.Element;
title?: string;
Expand All @@ -16,10 +15,7 @@ function WrapperRoute({ children, title, redirect }: WrapperRouteProps): JSX.Ele
document.title = title;
}
if (pathname === currentRouteAbsolutePath && redirect) {
const basePath = globalRouterBasePath.get();
const redirectPath = redirect.startsWith('/') ? basePath + redirect : redirect;

navigate(redirectPath);
navigate(redirect);
}
}, [currentRouteAbsolutePath, navigate, pathname, redirect, title]);

Expand Down
19 changes: 0 additions & 19 deletions packages/launcher/src/utils/globalEnv.ts

This file was deleted.

6 changes: 2 additions & 4 deletions packages/launcher/src/wrapperInit.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { BrowserRouter, HashRouter, Routes } from 'react-router-dom';
import renderRoutes from './router/render-routers';
import { Provider } from 'react-redux';
import { globalRouterBasePath } from './utils/globalEnv';
import type { RouteItem } from './app';
import type { Store, ReducerConfig, ReducerConfigItem } from './store';
import type { ReactElement } from 'react';
Expand Down Expand Up @@ -92,11 +91,10 @@ const routeWrapper = ({ hash, routes, routerBasePath }: RouteWrapperParamsType):
if (routerBasePath) {
basePath = routerBasePath;
}
globalRouterBasePath.set(basePath);

const APP = () => (
<Router>
<Routes basename={basePath}>{renderRoutes(routes, renderRoutesPluginWrapper)}</Routes>
<Router basename={basePath}>
<Routes>{renderRoutes(routes, renderRoutesPluginWrapper)}</Routes>
</Router>
);

Expand Down

0 comments on commit bcf760f

Please sign in to comment.